这让我感到非常紧张。这看起来很简单,我很可能错过了一些显而易见的东西 - 并强烈怀疑这是因为我的PHP/mysql技能缺乏,但我无法实现它。我在其他地方(并且被盗)了很多来自StackOverflow的代码片段,试图解决这个问题,但我仍然不确定我是否能够正常工作。从Xcode到JSON到PHP到mySql的阵列/字典
从Xcode我试图编码一个NSDictionary对象到JSON(使用JSON框架),以便我可以使用PHP POST方法动态地将数组存储在MySQL中(理想情况下作为单个扁平对象 - 我知道,我知道) 。
代码如下。我可以创建json。我可以连接好,我可以改变不需要通过json发送数组变量&的变量,我可以做任何事情。我似乎无法通过该JSON并将其存储在MySQL中。
是的,我是一个noob。
THX ...
我走了这么远:
在Xcode
NSDictionary *loginDict = [NSDictionary dictionaryWithObjectsAndKeys:
@"aname", @"username",
@"hello", @"password",
nil];
NSString *jsonString = [loginDict JSONRepresentation];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSString *post = [NSString stringWithFormat:@"json=%@", jsonString];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO];
[request setURL:[NSURL URLWithString:@"http://domain.com/post_dict.php"]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
在post_dict.php
<?php
$rawJsonData = $_POST['json'];
$decodedData = json_decode($rawJsonData); //do i even need to decode if i want to store a flattened json object in mysql?
//Connect To Database
$hostname='**BLACKEDOUT**.com';
$username='**BLACKEDOUT**';
$password='**BLACKEDOUT**';
$dbname='**BLACKEDOUT**';
$usertable='users';
//I want to update the Records field with the array
$recordsfield = 'Records';
mysql_connect($hostname,$username, $password) OR DIE ('Unable to connect to database! Please try again later.');
mysql_select_db($dbname);
$query = "UPDATE $usertable SET $recordsfield = '$decodedData' ";//do i encode? serialize? dunno
$result = mysql_query($query);
if(!$result)
{
mysql_close();
echo mysql_error();
return;
}
mysql_close();
?>
真棒 - 感谢快速反应 - 生病得到它。 – 2010-12-10 19:41:16