如何插入JSON数组到MySQL数据库 [英] How to insert json array into mysql database
问题描述
嗨我试图JSON数组插入到我的MySQL数据库。我传递数据形成我的iPhone在那里,我已经转换数据成JSON格式,我使用它不是插入我的服务器的URL传递数据到我的服务器。
这是我的JSON数据。
[{\"name\":\"0\",\"phone\":\"dsf\",\"city\":\"sdfsdf\",\"email\":\"dsf\"},{\"name\":\"13123123\",\"phone\":\"sdfsdfdsfsd\",\"city\":\"sdfsf\",\"email\":\"13123123\"}]
块引用>这是我的PHP code。
< PHP $ JSON =的file_get_contents('PHP://输入');
$ OBJ = json_de code($的数据,真实); //数据库连接
require_once'db.php中; / *插入数据到数据库* /
的foreach($ OBJ为$项目){
的mysql_query(INSERT INTO`数据库name`.`table name`(姓名,电话,城市,电子邮件)
VALUES('。$项目['名字'。''。$项目['电话'。''。$项目['城市'。''。$项目[ '电子邮件']。')); }
//数据库连接关闭
mysql_close($ CON); //}
?>我的数据库连接code。
< PHP //下面输入数据库连接信息:
$主机=localhost的;
$数据库=DBNAME;
$用户名=用户名;
$密码=密码; // DO NOT EDIT低于此线
$链接= mysql_connect($主机名,用户名$,$密码);
mysql_select_db($数据库)或死亡(无法选择数据库');
?>请告诉我哪里做错了上述code基本上我不是一个PHP开发人员,我的移动应用开发人员,所以我使用的PHP作为服务器端脚本,请告诉我如何解决这一问题。
解决方案$ JSON =的file_get_contents('PHP://输入');
$ OBJ = json_de code($ JSON,真正的);我觉得你错传递变量。你应该json_de code $传递JSON作为上述..
Hi I'm trying to insert the json array into my MySQL database. I'm passing the data form my iphone there i have converted the data into json format and I'm passing the data to my server using the url its not inserting into my server.
This is my json data.
[{"name":"0","phone":"dsf","city":"sdfsdf","email":"dsf"},{"name":"13123123","phone":"sdfsdfdsfsd","city":"sdfsf","email":"13123123"}]
This is my Php code.
<?php $json = file_get_contents('php://input'); $obj = json_decode($data,true); //Database Connection require_once 'db.php'; /* insert data into DB */ foreach($obj as $item) { mysql_query("INSERT INTO `database name`.`table name` (name, phone, city, email) VALUES ('".$item['name']."', '".$item['phone']."', '".$item['city']."', '".$item['email']."')"); } //database connection close mysql_close($con); //} ?>
My database connection code.
<?php //ENTER YOUR DATABASE CONNECTION INFO BELOW: $hostname="localhost"; $database="dbname"; $username="username"; $password="password"; //DO NOT EDIT BELOW THIS LINE $link = mysql_connect($hostname, $username, $password); mysql_select_db($database) or die('Could not select database'); ?>
Please tell where I'm doing wrong in the above code basically I'm not a php developer I'm mobile application developer so I'm using the php as a server side scripting please tell me how to resolve this problem.
解决方案$json = file_get_contents('php://input'); $obj = json_decode($json,true);
I think ur passing wrong variable. you should pass $json in json_decode as above..
这篇关于如何插入JSON数组到MySQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!