...

View Full Version : Resolved multiple queries with prepared statements



HDRebel88
07-12-2012, 06:01 AM
How would I get the number of queries successfully executed in a prepared statement?

I have this, and it's returning an error of:
Warning: mysqli_stmt_execute() expects parameter 1 to be mysqli_stmt, array given in /homepages/27/d418565624/htdocs/book/insert.php on line 8



<?php
require 'db_connect.php';
$array_values=array(array("test", "test"), array("Kay", "example"), array("kena","194"));
$prepared_query=mysqli_prepare($db, "INSERT INTO `users` (`username`, `password`)
VALUES (?, ?)");
$i=0;
foreach($array_values as $row_value){
mysqli_stmt_execute($row_value);
if(mysqli_stmt_affected_rows()==1){
$i++;
}
}

if($i==3){
echo "Data inserted.";
}
else{
echo "Data not Inserted.";
}
?>

HDRebel88
07-12-2012, 07:20 AM
Solved with the following:



<?php
require 'db_connect.php';
$array_values=array( array("test", "test"), array("Kayci", "example"), array("kena","194") );
$prepared_query=mysqli_prepare($db, "INSERT INTO `users` (`username`, `password`)
VALUES (?, ?)");

mysqli_stmt_bind_param($prepared_query, 'ss', $username, $password);

foreach($array_values as $row_value){
$username=$row_value[0];
$password=$row_value[1];
mysqli_stmt_execute($prepared_query);
$num_affected+=mysqli_stmt_affected_rows($prepared_query);
}

if($num_affected==3){
echo "Data inserted.";
}
else{
echo "Data not Inserted.";
}
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum