运行Json到MySQL时,它可以很好地导入数据库。 当我手动运行SQL查询时,它运行无误。
在通过代码运行它时,它返回并在第1行附近出现我的SQL语法错误。
我删除了魔术引号,仍然是错误。 在我戴上前,有人可以照亮它吗?
我知道魔术引号已被弃用,我认为这是导致错误的原因,但是这些引号已被删除。
<?php
/**
* Created by PhpStorm.
* User: dave
* Date: 14/11/2018
* Time: 15:26
*/
$connect = mysqli_connect("localhost", "root", "password", "clients");
$query = '';
$table_data = '';
$filename = "test.json";
$data = file_get_contents($filename);
$array = json_decode($data, true);
foreach($array as $set)
{
$tblName = $set['tableName'];
echo $tblName ;
if(sizeof($set['rows']) > 0) {
$query = '';
$colList = array();
$valList = array();
// Get list of column names
foreach($set['rows'][1] as $colname => $dataval) {
// $colList[] = "`".$colname."`";
$colList[] = $colname;
}
$query .= "INSERT INTO ".$tblName." \n";
$query .= "(".implode(", ",$colList).")\nVALUES\n";
// Go through the rows for this table.
foreach($set['rows'] as $idx => $row) {
$colDataA = array();
// Get the data values for this row.
foreach($row as $colName => $colData) {
$colDataA[] = "'".$colData."'";
}
$valList[] = "(".implode(",",$colDataA).")";
}
// Add values to the query.
$query .= implode(",\n",$valList)."\n";
echo "<p>Insert query:<pre>$query</pre></p>";
$results = mysqli_query($connect, $query);
// if ($connect->query($results) === TRUE) {
// echo "New record created successfully";
// } else {
// echo $results . "<br>" . $query . "<br>" ;
// echo "error" .$connect->error ;
// }
echo "<h1>".mysqli_num_rows($connect)." Rows appeded in $tblName</h1>";
} else {
echo "<p>No rows to insert for $tblName</p>";
}
}
Json文件:
[
{
"tableName":"contacts",
"rows":[
{
"First_Name": "Dave",
"Last_Name": "Frank",
"Company": "Company1",
"Business_Phone": "0115 999999",
"Email_Address": "zvv@zz.com"
},
{
"First_Name": "Dave",
"Last_Name": "Blogs",
"Company": "Company2",
"Business_Phone": "0115 888888",
"Email_Address": "zvv@zz.com"
},
{
"First_Name": "David",
"Last_Name": "frank",
"Company": "Company3",
"Business_Phone": "0115 777777",
"Email_Address": "zvv@zz.com"
}
]
},
{
"tableName":"contacts_old",
"rows":[
{
"First_Name": "Dave",
"Last_Name": "Frank",
"Company": "Company1",
"Business_Phone": "0115 999999",
"Email_Address": "zvv@zz.com"
},
{
"First_Name": "Dave",
"Last_Name": "Blogs",
"Company": "Company2",
"Business_Phone": "0115 888888",
"Email_Address": "zvv@zz.com"
},
{
"First_Name": "David",
"Last_Name": "frank",
"Company": "Company3",
"Business_Phone": "0115 777777",
"Email_Address": "zvv@zz.com"
}
]
}
]
以下是根据请求的回显:
contacts
Insert query:
INSERT INTO contacts
(First_Name, Last_Name, Company, Business_Phone, Email_Address)
VALUES
('Dave','Frank','Company1','0115 999999','zvv@zz.com'),
('Dave','Blogs','Company2','0115 888888','zvv@zz.com'),
('David','frank','Company3','0115 777777','zvv@zz.com')
Error: 1
INSERT INTO contacts (First_Name, Last_Name, Company, Business_Phone, Email_Address) VALUES ('Dave','Frank','Company1','0115 999999','zvv@zz.com'), ('Dave','Blogs','Company2','0115 888888','zvv@zz.com'), ('David','frank','Company3','0115 777777','zvv@zz.com')
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1contacts_old
Insert query:
INSERT INTO contacts_old
(First_Name, Last_Name, Company, Business_Phone, Email_Address)
VALUES
('Dave','Frank','Company1','0115 999999','zvv@zz.com'),
('Dave','Blogs','Company2','0115 888888','zvv@zz.com'),
('David','frank','Company3','0115 777777','zvv@zz.com')
Error: 1
INSERT INTO contacts_old (First_Name, Last_Name, Company, Business_Phone, Email_Address) VALUES ('Dave','Frank','Company1','0115 999999','zvv@zz.com'), ('Dave','Blogs','Company2','0115 888888','zvv@zz.com'), ('David','frank','Company3','0115 777777','zvv@zz.com')
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1
警告:mysqli_num_rows()期望参数1为mysqli_result,布尔值在
中给出答案 0 :(得分:2)
因此,正如奈杰尔(Nigel)所指出的,您最初的问题是由于将结果集传递给query()
而不是字符串引起的。我希望这个问题会因错别字而被关闭,但我想在此之前指出您的代码中存在许多问题和效率低下。
最重要的是,由于缺乏准备好的语句和数据清理功能,因此一旦出现撇号,查询就会崩溃。我对mysqli感到生疏,所以我在这里的代码中使用了PDO,因为我可以轻松地做到这一点。无论如何,这是一个更现代的界面,而且冗长得多。您可以在PDO和mysqli中阅读有关准备好的语句的更多信息,但是要记住的重要一点是,您准备使用?
作为将要使用的值的占位符来准备语句插入。然后,执行语句,将值传递给数据库。数据库负责转义任何讨厌的值,并且比在每次循环迭代时重建整个查询要少得多的开销。
您还应该寻求使用array_keys
,array_values
或array_columns
之类的PHP内置函数来提取数据,而不是依靠foreach
循环。 / p>
<?php
$connect = new PDO("mysql:host=localhost;dbname=clients", "root", "password");
$filename = "test.json";
$data = file_get_contents($filename);
$array = json_decode($data, true);
foreach($array as $set)
{
$tblName = $set['tableName'];
if(count($set['rows']) > 0) { /* typical to use count() for measuring array size */
// Get list of column names
$colList = array_keys($set["rows"][0]); /* no loop needed */
$query = "INSERT INTO `$tblName` \n";
$query .= "(" . implode(", ", $colList) . ")\nVALUES\n";
$placeholders = implode(",", array_fill(0, count($colList), "?"));
$query .= "($placeholders)"; /* you should have INSERT INTO xxx (yyy, zzz) VALUES (?, ?) */
$stmt = $connect->prepare($query);
if (!$stmt) {
echo "Prepare error: $query<br/>" . $connect->errorInfo()[2];
continue; /* give up, go to next record set */
}
// Go through the rows for this table.
foreach($set['rows'] as $row) {
$result = $stmt->execute($row); /* hey you can just pass the array directly! */
if ($result === TRUE) {
echo "New record created successfully";
} else {
echo "Execute error: $stmt->queryString<br/>" . $stmt->errorInfo()[2];
}
}
} else {
echo "<p>No rows to insert for $tblName</p>";
}
}