DALLASCAO.COM

Site of Cao Shouguang, English to Chinese translator

The idea of mysqli prepare

Notice you do not need to prepare for each row insertion.


$db = new mysqli('localhost', 'dbusername', 'dbpassword', 'dbname');
if ($db->connect_error >0) {
    die('Unable to connect to database [' . $db->connect_error . ']');
}
$sql1 = <<query($sql1); 
$sql2 = <<prepare($sql2);
//you do not need to prepare for each row insertion.


while ($row=$result->fetch_row()) {
    $i++;
    $email=$row[0]; $firstName = $row[1]; $lastName =$row[2]; $beginDate=$row[3]; $CancelDate=$row[4];
    $insert_result->bind_param('sssss', $email, $firstName, $lastName, $beginDate, $CancelDate);
    $insert_result->execute();
}

This will work but the “prepare” is done for each insertion and will be slower.


$db = new mysqli('localhost', 'dbusername', 'dbpassword', 'dbname');
if ($db->connect_error >0) {
    die('Unable to connect to database [' . $db->connect_error . ']');
}
$sql1 = <<query($sql1);
while ($row=$result->fetch_row()) {

    $email=$row[0]; $firstName = $row[1]; $lastName =$row[2]; $beginDate=$row[3]; $CancelDate=$row[4];    
    $sql2 = <<prepare($sql2);

    $insert_result->bind_param('sssss', $email, $firstName, $lastName, $beginDate, $CancelDate);
    $insert_result->execute();
}

Leave a Comment