[英]How can I use php to go through this json and add the items inside it to a mysqli database?
I am trying to allow an android user to replace a database table. 我正在尝试允许android用户替换数据库表。 My app reads in a excel spreadsheet from the phone, adds each row into a json, and then serializes the json to get a string:
我的应用程序从手机读取excel电子表格,将每行添加到json,然后序列化json以获取字符串:
[{"name":"catheter","alts":"cathater, cathiter, cath","num":"134"},
{"name":"cast","alts":"","num":"2212"}]
My app then sends the serializes json to a php file on my server where I 然后,我的应用将序列化json发送到服务器上的php文件中,
var_dump(json_decode($serializedString));
to get to the array in question: 进入有问题的数组:
array(2) { [0]=> object(stdClass)#2 (4) { ["name"]=> string(8)
"catheter" ["alts"]=> string(24) "cathater, cathiter, cath"
["num"]=> string(3) "134" } [1]=> object(stdClass)#3 (4) {
["name"]=> string(4) "cast" ["alts"]=> string(0) ""
["number"]=> string(0) "" }}
I'm php stupid, but I have tried a BUNCH of stuff. 我是php愚蠢的人,但是我尝试了很多东西。 This will tell me how many items there are in the array:
这将告诉我数组中有多少个项目:
$count = 0;
$something = json_decode($serializedJsonString, true);
foreach($something as $obj){
$count = $count+1;
}
echo $count;
I really need to be able to refer to each items key-value pairs so I can add them to a database which has columns for name, alt_names, and number. 我确实需要能够引用每个项目的键值对,以便将它们添加到具有名称,alt_names和数字列的数据库中。 Thanks a bundle.
谢谢捆绑。
May be this can help you : 也许这可以帮助您:
$serializedString= '[{"name":"catheter","alts":"cathater, cathiter, cath","num":"134"},
{"name":"cast","alts":"","num":"2212"}]';
$res = json_decode($serializedString, true);
function for isert data (call it while looping the array) 用于isert数据的函数(在循环数组时调用它)
function insert($element , $mysqli){
if(isset($element['name']) && isset($element['alts']) && isset($element['num'])){
$stmt = $mysqli->prepare("INSERT INTO myTable (name, alt_names,number ) VALUES (?, ?, ?)");
$stmt->bind_param($element['name'], $element['alts'],$element['num']);
$stmt->execute();
$stmt->close();
}
}
your main script : 您的主要脚本:
$host = 'insert here the host';
$db = 'name of your database';
$username= 'db username';
$password = 'password db';
// create database connection
$mysqli = new mysqli($host, $username, $password, $db);
$mysqli->set_charset("utf8mb4");
$mysqli->autocommit(FALSE); // manage transaction
foreach($res as $key => $value){
insert($value,$mysqli);
}
$mysqli->commit();
$count = 0;
$something = json_decode($serializedJsonString, true);
foreach($arr as $item){
$name = $item["name"];
$alts = $item["alts"];
$num = $item["num"];
//make insert query here.
}
You could do something like this: 您可以执行以下操作:
$db = new MySQLi(HOST, USER, PASS, SCHEMA, PORT);
$count = 0;
$elems = json_decode($serializedJsonString, true);
$stmt = $db->prepare('INSERT INTO items (name, alt_names, number) VALUES (?,?,?)');
$db->begin_transaction();
foreach($elems as $elem){
$stmt->bind_param("ssi", $elem['name'], $elem['alts'], $elem['num']);
$stmt->execute();
$count = $count+1;
}
$stmt->close();
$db->commit();
$db->close();
echo $count;
This is also safe against any SQL injection . 这对于任何SQL注入也是安全的。 And I've supposed that location and id are nullable and not available at the moment of this operation.
而且我认为location和id可为空,并且在执行此操作时不可用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.