从 Xcode 到 JSON 到 PHP 到 mySql 的数组/字典

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4411967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:59:51  来源:igfitidea点击:

Array/Dict From Xcode to JSON to PHP to mySql

phpiphonemysqlxcodejson

提问by James Jesst

This is driving me nuts. It seems so simple, and I am likely missing something obvious - and strongly suspect it's because my PHP/mysql skills are lacking, but I can't get it to work. I have looked hard elsewhere (and stolen) a number of code snippets from StackOverflow trying to solve the issue, but I'm still not convinced I've got it working.

这让我发疯。看起来很简单,我可能遗漏了一些明显的东西 - 并且强烈怀疑这是因为我缺乏 PHP/mysql 技能,但我无法让它工作。我已经在其他地方努力寻找(并偷走了)来自 StackOverflow 的许多代码片段试图解决这个问题,但我仍然不相信我已经让它工作了。

From Xcode I'm trying to encode an NSDictionary object into JSON (using JSON framework) so that I can dynamically store the array in mysql (ideally as a single flattened object - I know, I know) using PHP POST method.

在 Xcode 中,我试图将 NSDictionary 对象编码为 JSON(使用 JSON 框架),以便我可以使用 PHP POST 方法将数组动态存储在 mysql(理想情况下作为单个扁平对象 - 我知道,我知道)。

Code is below. I can create the json ok. I can connect ok, I can change variables that arent array variables & that do not need to be send via json, I can do just about anything. I can't seem to pass that json and store it in mysql.

代码如下。我可以创建json。我可以正常连接,我可以更改不是数组变量和不需要通过 json 发送的变量,我可以做任何事情。我似乎无法传递该 json 并将其存储在 mysql 中。

Yes- im a noob.

是的 - 我是个菜鸟。

Thx...

谢谢...

I've got this far:

我已经走了这么远:

in xcode

在 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];

at post_dict.php

在 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();


?>

回答by Nakkeeran

If your are sending your JSON in POST method , It can be received in PHP with the below code

如果您以 POST 方法发送 JSON,则可以使用以下代码在 PHP 中接收

<?php $handle = fopen('php://input','r');
                $jsonInput = fgets($handle);
                // Decoding JSON into an Array
                $decoded = json_decode($jsonInput,true);
?>

回答by Jonathan Amend

To answer your comment: //do i even need to decode if i want to store a flattened json object in mysql?

要回答您的评论: //do i even need to decode if i want to store a flattened json object in mysql?

No, you should not json_decode()the data; instead save the $rawJsonDatato MySQL. You should however escape it using mysql_real_escape_string(). Here's an example:

不,你不应该json_decode()数据;而是将其保存$rawJsonData到 MySQL。但是,您应该使用mysql_real_escape_string(). 下面是一个例子:

$rawJsonData = $_POST['json']; //i don't 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);

//Escape the JSON data for MySQL
$mysqlEncodedJsonData = mysql_real_escape_string($rawJsonData);

$query = "UPDATE $usertable SET $recordsfield = '$mysqlEncodedJsonData' ";//inserted variable should be mysql_real_escape_string()'d as it is above

$result = mysql_query($query);

if(!$result) { mysql_close(); echo mysql_error(); return; }

mysql_close();