每次在循环中使用php mysqli绑定数组参数

时间:2018-04-07 02:46:02

标签: php mysqli

我有一个循环,我必须对数据库进行多次插入,看来我每次获取新数据和数据更改时都必须绑定数据数组。我虽然可以绑定一次并插入多次。

进一步的研究表明,PDO可能更适合这项任务,但我很好奇为什么它与mysqli一样,如果有一种方法可以将数组绑定一次并执行多次。

请参阅代码中的注释以获取更多详细信息。

<?php
//usual db stuff
$DBhost = 'localhost';
$DBname = 'test';
$DBuser = '';
$DBpass = '';

//error reporting for mysql
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

//connect to database
$conn = new mysqli($DBhost, $DBuser, $DBpass, $DBname);

//prepare this is a dummy table in a dummy database
//actual table is quite complicated
$stmt = $conn->prepare('INSERT INTO `users` (`fname`,`lname`,`city`) VALUES(?,?,?)');

//create a variable to store the data
// so we can bind it before going further
$data=array_fill(0,3,'');
//bind params use array expansion
$stmt->bind_param('sss', ...$data);

//this part runs in a loop
// mock loop for just to show idea
for($i=0;$i<1;$i++)
{
    //get the data
    $data = get_data();
    //already binded $data before the loop
    //so execute this should insert the data just returned
    //except this doesnt, it inserts blanks
    //it acts as if the data still has the blank array from line 20
    $stmt->execute();

    //bind it again
    $stmt->bind_param('sss', ...$data);
    //execute
    //this inserts the data
    $stmt->execute();
}

//mock function to return data the actual function 
//returns a multi dimesional array for multiple tables
function get_data()
{
    return(array("dinesh","chand","nadi"));
}

高度赞赏的任何帮助或想法

1 个答案:

答案 0 :(得分:0)

第一次运行$stmt->execute();时,在for循环中,您在启动for loop之前执行绑定的内容。所以你要做的就是。

  1. 绑定您想要的内容$stmt->bind_param('sss', ...$data);
  2. 然后执行$stmt->execute();
  3. 在上面的代码中,首先删除$stmt->execute();(首先在for loop内)。然后在第一次绑定参数后(for loop上方)添加它。如下,

    注意:我更改的内容标有########

    <?php
    //usual db stuff
    $DBhost = 'localhost';
    $DBname = 'test';
    $DBuser = '';
    $DBpass = '';
    
    //error reporting for mysql
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    
    //connect to database
    $conn = new mysqli($DBhost, $DBuser, $DBpass, $DBname);
    
    //prepare this is a dummy table in a dummy database
    //actual table is quite complicated
    $stmt = $conn->prepare('INSERT INTO `users` (`fname`,`lname`,`city`) VALUES(?,?,?)');
    
    //create a variable to store the data
    // so we can bind it before going further
    $data=array_fill(0,3,'');
    //bind params use array expansion
    $stmt->bind_param('sss', ...$data);
    
    $stmt->execute(); //########
    
    //this part runs in a loop
    // mock loop for just to show idea
    for($i=0;$i<1;$i++)
    {
        //get the data
        $data = get_data();
        //already binded $data before the loop
        //so execute this should insert the data just returned
        //except this doesnt, it inserts blanks
        //it acts as if the data still has the blank array from line 20
        //$stmt->execute(); ########
    
        //bind it again
        $stmt->bind_param('sss', ...$data);
        //execute
        //this inserts the data
        $stmt->execute();
    }