3

我想要做的是将阿拉伯文本保存在 My SQL 中。

我的 SQL 表的排序规则为utf8_general_ci

我在 iPhone 应用程序中有文本字段,我正在使用 PHP 保存数据。

PHP 代码如下所示。

$con = mysql_connect(localhost, $username, $password);
@mysql_select_db($database) or die("Unable to select database");


$propType = $_POST['propType'];
$price = $_POST['price'];
$type = $_POST['type'];
$zone = $_POST['zone'];
$location = $_POST['location'];
$no_floor = $_POST['no_floor'];
$no_apt = $_POST['no_apt'];
$basement = $_POST['basement'];
$area = $_POST['area'];
$addedBy = $_POST['addedBy'];
$imagePath = $_POST['imagePath'];
$saleType = $_POST['dummy001'];
$mauka = $_POST['mauka'];
$mobileNumForDial = "0";


$result = mysql_query("SELECT mobileNumber FROM userInfo WHERE username='$addedBy'");
while($data = mysql_fetch_row($result)){
    $mobileNumForDial=$data[0];
}

$sql = mysql_query("INSERT INTO buildingData (propType, price, type, zone, location, no_floor,
        no_apt, basement, area, addedBy, imagePath, dummy001, dummy002, mauka) 
        VALUES 
        ('$propType', '$price', '$type', '$zone', '$location', '$no_floor', '$no_apt', '$basement', 
        '$area','$mobileNumForDial','$imagePath', '$saleType', '', '$mauka')");

iPhone 代码如下所示

if ([btnImage.accessibilityIdentifier isEqualToString:@"no_image"]) {
    imageSet = @"missing";
} else {
    imageSet = @"set";
}
post = [[NSString alloc] initWithFormat:@"propType=%d&price=%@&type=%d&zone=%d&location=%d&no_floor=%d&no_apt=%d&basement=%d&area=%@&addedBy=%@&imagePath=%@&dummy001=%d&mauka=%@",
        realEstatePV.selectedItem,
        priceData.text,
        typePV.selectedItem,
        areaPV.selectedItem,
        regionPV.selectedItem,
        floorData.text.intValue,
        aptData.text.intValue,
        basementPV.selectedItem,
        areaData.text,
        [keychainItem objectForKey:kSecAttrAccount],
        imageSet, salePV.selectedItem, mauka.text];
postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

postLength = [NSString stringWithFormat:@"%d", [postData length]];

url = [NSURL URLWithString:@"http://www.smart-kw.com/sama/saveBldgData.php"];

NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
[theRequest setHTTPMethod:@"POST"];
[theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPBody:postData];


NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

if( theConnection )
{
    indicator.hidden = NO;
    [indicator startAnimating];
    webData = [[NSMutableData data] retain];
}
else
{
    NSLog(@"Internet problem maybe...");
}

问题是

当我存储英文数据时,它很好。

但是,当我输入阿拉伯语文本时,它会存储为 ??? 在 mysql 数据库中。

知道为什么会这样吗?


编辑 1

我也尝试过以下但仍然数据存储为*???

mysql_query("SET NAMES 'utf8'"); 
mysql_query('SET CHARACTER SET utf8');

$sql = mysql_query("INSERT INTO buildingData (propType, price, type, zone, location, no_floor,
        no_apt, basement, area, addedBy, imagePath, dummy001, dummy002, mauka) 
        VALUES 
        ('$propType', '$price', '$type', '$zone', '$location', '$no_floor', '$no_apt', '$basement', 
        '$area','$mobileNumForDial','$imagePath', '$saleType', '', '$mauka')");
4

2 回答 2

3

尝试:

[theRequest setValue:@"charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
于 2013-06-12T09:22:20.943 回答
0

尝试将 postData 设置为使用 UTF 8 编码而不是 ASCII 编码:

postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
于 2013-06-13T02:04:02.917 回答