简体   繁体   English

如何将数组中的数据存储到mySQL数据库中?

[英]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.

相关问题 如何将mySQL数据库中的数据存储到数组中? - How can I store data from mySQL database into an array? 如何将MySQL数据库中的数据存储到ID索引的数组中? - How can I store data from MySQL database into array indexed by id? 如何将区域语言 php 数组数据(古吉拉特语、印地语、旁遮普语)存储和检索到 mysql 数据库? - How can I store and retrieve regional language php array data (gujarati, hindi, punjabi) to mysql database? 如何使用mysql php将数组值存储在数据库中 - how can i store the array values in database using mysql php 如何在php中从mysql数据库将数据存储为关联数组 - how to store data as an associative array from mysql database in php 如何将嵌套/多维数组中的值存储在 MySQL 数据库中? - How can I store values from a nested/multidimensional array in a MySQL database? 我如何将大量内容从多阵列数组存储到mysql数据库中? - How can I store a huge amount of content from multidiemsional array into mysql database? 如何在MySQL数据库中存储对象的颜色数据? - How can I store an object's color data in MySQL database? 如何将敏感数据安全地存储在MySQL数据库中? - How can I store sensitive data securely in a MySQL database? 如何使用PHP从另一个网站获取数据并将其存储在MySQL数据库中? - How can I use PHP to fetch data from another website and store it in a MySQL database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM