我正在研究基于数据的大型项目。我为PHP中的每个数据创建了数组。现在我想将它插入到MySQL数据库中。
<?php
$servername = "localhost";
$username = "xxxx";
$password = "xxxxx";
$db_name = "xxxxxxx";
// Create connection
$link = mysql_connect($servername, $username, $password);
if(!$link){
echo "Failed" .mysql_connect_error();
}
mysql_select_db($db_name);
$data = array (
[1] => array(
'eng_title' => "john",
'content' => "john",
'category' => "john",
),
[2] => array(
'eng_title' => "john",
'content' => "john",
'category' => "john",
),
[3] => array(
'eng_title' => "john",
'content' => "john",
'category' => "john",
),
);
if(is_array($data)){
$sql = "INSERT INTO `table_a`( `eng_title`, `content`, `category`) values";
$valuesArr = array();
foreach($data as $row){
$eng_title = mysql_real_escape_string( $row['eng_title'] );
$content = mysql_real_escape_string( $row['content'] );
$category = mysql_real_escape_string( $row['category'] );
$valuesArr[] = "('$eng_title', '$content', '$category')";
}
$sql .= implode(',', $valuesArr);
echo $sql;
mysql_query($sql) or exit(mysql_error());
}
?>
这是执行代码时出现的消息。
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 '' at line 1
我在其他程序中使用MySQLi,我想我使用最新版本的MySQL,这就是为什么上述程序无效。
答案 0 :(得分:0)
因为我认为你在“内爆”中遗漏了一些东西。 试试这个 -
if(is_array($data)){
$sql = "INSERT INTO `table_a`( `eng_title`, `content`, `category`) values";
$valuesArr = array();
$last=count($valuesArr);
$i=0;
foreach($data as $row){
$eng_title = mysql_real_escape_string( $row['eng_title'] );
$content = mysql_real_escape_string( $row['content'] );
$category = mysql_real_escape_string( $row['category'] );
$sql .= "('$eng_title', '$content', '$category')";
if($i<$last)
$sql .=" , ";
$i++;
}
echo $sql;
mysql_query($sql) or exit(mysql_error());
}
答案 1 :(得分:0)
使用PDO,最好看一下:Choosnig an API
要使用PDO执行insert语句,您可以在此处查看更多示例:PDO - Manual
因此,对于您的情况,您可以通过两种方式实现:
<强> 1。在每个循环上插入一行:
<?php
$data = array (
1 => array(
'eng_title' => "john",
'content' => "john",
'category' => "john",
),
2 => array(
'eng_title' => "john",
'content' => "john",
'category' => "john",
),
3 => array(
'eng_title' => "john",
'content' => "john",
'category' => "john",
),
);
$servername = "localhost";
$username = "xxxx";
$password = "xxxxx";
$db_name = "xxxxxxx";
$pdo = new PDO("mysql:host=$servername;dbname=$db_name", "$username", "$password");
foreach ($data as $key => $value) {
$eng_title = mysql_real_escape_string( $value['eng_title'] );
$content = mysql_real_escape_string( $value['content'] );
$category = mysql_real_escape_string( $value['category'] );
$query = $db->prepare('INSERT INTO table_a(eng_title, content, category) VALUES(:title, :content, :category)');
$query->bindParam("title", $eng_title);
$query->bindParam("content", $content);
$query->bindParam("category", $category);
$query->execute();
}
<强> 2。创建一个SQL语句并执行它:
<?php
$data = array (
1 => array(
'eng_title' => "john",
'content' => "john",
'category' => "john",
),
2 => array(
'eng_title' => "john",
'content' => "john",
'category' => "john",
),
3 => array(
'eng_title' => "john",
'content' => "john",
'category' => "john",
),
);
$servername = "localhost";
$username = "xxxx";
$password = "xxxxx";
$db_name = "xxxxxxx";
$pdo = new PDO("mysql:host=$servername;dbname=$db_name", "$username", "$password");
$query = "INSERT INTO table_a(eng_title, content, category) VALUES";
$counter = 0;
$arraySize = sizeof($data);
foreach ($data as $key => $value) {
$eng_title = mysql_real_escape_string( $value['eng_title'] );
$content = mysql_real_escape_string( $value['content'] );
$category = mysql_real_escape_string( $value['category'] );
$counter++;
$query .= "($eng_title, $content, $category)";
if($counter < $arraySize)
$query .= ",";
}
$pdo->query($query);
希望它会对你有所帮助。