[英]How can I store data from an array into the mySQL database?
I want to store data from an array into my database. 我想将数据从数组存储到数据库中。
This is how my Array looks when I do print_r($animals);
这就是我执行
print_r($animals);
时的数组外观print_r($animals);
: :
Array
(
[0] => Array
(
[animal] => Cat
[name] => Tom
)
[1] => Array
(
[animal] => Dog
[name] => Bob
)
[2] => Array
(
[animal] => Bird
[name] => Sam
)
[3] => ....
This is how I try to store the data, but for some reason it doesn't work, nothing is stored: 这是我尝试存储数据的方式,但是由于某种原因它不起作用,因此什么也没有存储:
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO animals (animal,name) values(?,?) ";
$q = $pdo->prepare($sql);
foreach($animals as $row) {
$q->execute(array(
$row['animal'],
$row['name'],));
}
Database::disconnect();
If I write the following for example the storing works, but only the first entry is stored (Cat and Tom) 例如,如果我写以下内容,则表示存储有效,但是仅存储了第一个条目(Cat和Tom)
foreach($animals as $row) {
$name = $row['name'];
$animal = $row['animal'];
}
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO animals (animal,name) values(?,?) ";
$q = $pdo->prepare($sql);
$q->execute(array($animal,$name));
Database::disconnect();
Your first attempt made more sense. 您的第一次尝试更有意义。 Remember you only need to
prepare
a parameterised statement once, then you can run it 1000 times if you like as long as you replace the parametes each time. 请记住,您只需
prepare
一次参数化语句,然后只要您每次替换参数,便可以运行1000次。
Also as you are setting PDO to emit Exceptions on error you should code this in a Try/Catch block 同样,当您将PDO设置为在错误时发出异常时,应在Try / Catch块中对此进行编码
try {
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO animals (animal,name) values(?,?) ";
$q = $pdo->prepare($sql);
foreach($animals as $row) {
$q->execute(array($row['animal'], $row['name']));
}
}
catch (PDOException $pe) {
echo $pe->getMessage();
}
catch (Exception $e ) {
echo $e->getMessage();
}
Database::disconnect();
Modify your code like this, it will work. 像这样修改您的代码,它将起作用。
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
foreach($animals as $row) {
$sql = "INSERT INTO animals (animal,name) values($row['animal'], $row['name']) ";
$q = $pdo->prepare($sql);
$q->execute();
}
Database::disconnect();
I can't explain it well, but I know the CodeIgniter Framework used PDO this way for SQL execution. 我不能很好地解释它,但是我知道CodeIgniter框架以这种方式将PDO用于SQL执行。
The first one you got syntax error extra comma: 第一个语法错误用逗号隔开:
foreach($animals as $row) {
$q->execute(array(
$row['animal'],
$row['name'],));//extra comma
}
The second one you got the loop at the top overwriting the variables. 第二个是您在顶部覆盖了变量的循环。 This means it will insert the last row.
这意味着它将插入最后一行。
So remove the comma in that for each or use for loop: 因此,请删除其中的逗号或使用for循环:
for($i=0; $i< count($animals); $i++) {
$q->execute(array(
$animals[$i]['animal'],
$animals[$i]['name']));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.