我编写了这个PHP代码,用于从JSON文件中提取值并将它们插入到MySQL数据库中。
<?php
//connect to mysql db
$con = mysqli_connect("localhost","root","","db_tweets") or die('Could not connect: ' . mysql_error());
//read the json file contents
$jsondata = file_get_contents('prova.json');
//convert json object to php associative array
$data = json_decode($jsondata, true);
foreach ($data as $u => $z){
foreach ($z as $n => $line){
//get the tweet details
$text = $line['text'];
$id_tweet = $line['id_str'];
$date = $line['created_at'];
$id_user = $line['user']['id_str'];
$screen_name = $line['user']['screen_name'];
$name = $line['user']['name'];
$sqlu = "INSERT INTO user(id_user, screen_name, name)
VALUES ('".$id_user."', '".$screen_name."', '".$name."')";
}
}
if(!mysqli_query($con, $sqlu))
{
die('Error : ' . mysql_error());
}
?>
这样做会将值始终插入表格的第一行,覆盖以前的值。所以它仍然只是最后一个。
我怎么能:
1)将所有值插入多行?
2)解析多个JSON文件?
答案 0 :(得分:1)
试试这个。
您只是在执行最后一个查询,因为mysqli_query()
在外部循环。
方法1:
<?php
//connect to mysql db
$con = mysqli_connect("localhost","root","","db_tweets") or die('Could not connect: ' . mysql_error());
//read the json file contents
$jsondata = file_get_contents('prova.json');
//convert json object to php associative array
$data = json_decode($jsondata, true);
foreach ($data as $u => $z){
foreach ($z as $n => $line){
//get the tweet details
$text = $line['text'];
$id_tweet = $line['id_str'];
$date = $line['created_at'];
$id_user = $line['user']['id_str'];
$screen_name = $line['user']['screen_name'];
$name = $line['user']['name'];
$sqlu = "INSERT INTO user(id_user, screen_name, name)
VALUES ('".$id_user."', '".$screen_name."', '".$name."')";
if(!mysqli_query($con, $sqlu))
{
die('Error : ' . mysql_error());
}
}
}
?>
方法2:
<?php
//connect to mysql db
$con = mysqli_connect("localhost","root","","db_tweets") or die('Could not connect: ' . mysql_error());
//read the json file contents
$jsondata = file_get_contents('prova.json');
//convert json object to php associative array
$data = json_decode($jsondata, true);
$values = "";
foreach ($data as $u => $z){
foreach ($z as $n => $line){
//get the tweet details
$text = $line['text'];
$id_tweet = $line['id_str'];
$date = $line['created_at'];
$id_user = $line['user']['id_str'];
$screen_name = $line['user']['screen_name'];
$name = $line['user']['name'];
$values .= "('".$id_user."', '".$screen_name."', '".$name."'),";
}
}
if(!empty($values)) {
$values = substr($values, 0, -1);
$sqlu = "INSERT INTO user(id_user, screen_name, name) VALUES {$values}";
if(!mysqli_query($con, $sqlu))
{
die('Error : ' . mysql_error());
}
}
?>
回答多个文件:
<?php
//connect to mysql db
$con = mysqli_connect("localhost","root","","db_tweets") or die('Could not connect: ' . mysql_error());
$files = array("prova.json", "file2.json");
foreach ($files as $file) {
//read the json file contents
$jsondata = file_get_contents($file);
//convert json object to php associative array
$data = json_decode($jsondata, true);
$values = "";
foreach ($data as $u => $z) {
foreach ($z as $n => $line) {
//get the tweet details
$text = $line['text'];
$id_tweet = $line['id_str'];
$date = $line['created_at'];
$id_user = $line['user']['id_str'];
$screen_name = $line['user']['screen_name'];
$name = $line['user']['name'];
$values .= "('" . $id_user . "', '" . $screen_name . "', '" . $name . "'),";
}
}
if (!empty($values)) {
$values = substr($values, 0, -1);
$sqlu = "INSERT INTO user(id_user, screen_name, name) VALUES {$values}";
if (!mysqli_query($con, $sqlu)) {
die('Error : ' . mysql_error());
}
}
}
?>
答案 1 :(得分:0)
对于每个循环,在循环之后将该变量传递给$sqlu
函数之前,您将覆盖最后的mysqli_query
值。因此,一旦完成循环,您就会将最后指定的值留给$sqlu
,并且这是唯一被执行的值。
相反,在循环中执行查询并...
使用PHP mysqli_
函数mysqli_prepare
,mysqli_stmt_bind_param
和mysqli_stmt_execute
来简化和保护您的查询:
//connect to mysql db
$con = mysqli_connect("localhost","root","","db_tweets") or die('Could not connect: ' . mysql_error());
// prepare your insert query
$stmt = mysqli_prepare($con, 'INSERT INTO user(id_user, screen_name, name) VALUES (?, ?, ?)');
// bind the upcoming variable names to the query statement
mysqli_stmt_bind_param($stmt, 'iss', $id_user, $screen_name, $name);
// loop over JSON files
$jsonfiles = array('prova.json', 'provb.json', 'provc.json');
foreach ( $jsonfiles as $jsonfile ) {
//read the json file contents
$jsondata = file_get_contents($jsonfile);
//convert json object to php associative array
$data = json_decode($jsondata, true);
foreach ($data as $u => $z){
foreach ($z as $n => $line){
//get the tweet details
$id_user = $line['user']['id_str'];
$screen_name = $line['user']['screen_name'];
$name = $line['user']['name'];
// execute this insertion
mysqli_stmt_execute($stmt);
}
}
}
因此,这不仅可以通过准备一次查询来使用更少的数据库资源,并且具有更清晰的代码,而且还可以正确地转义插入值以防止sql注入。
添加了一个包含任意数量的JSON文件名的数组$jsonfiles
,并使用foreach
构造循环遍历JSON文件。