I have the following test script to adapt all my current programs to using prepared statements ... can't find the right syntax/structure to read the result rows:
$userid = "admin";
$stmt = mysqli_stmt_init($link);
if (mysqli_stmt_prepare($stmt, 'SELECT * FROM user_info WHERE userid = ?')) {
mysqli_stmt_bind_param($stmt, "s", $userid);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_store_result($stmt);
$number_of_rows = mysqli_stmt_num_rows($stmt);
echo "Number of Rows: $number_of_rows<br />";
$result = mysqli_stmt_get_result($stmt);
for($i=0;$i<$number_of_rows;$i++){
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
echo $row["id"];
}
mysqli_stmt_close($stmt);
}
else{
// Catch a database error here
die("Could not query database.");
}
How do I reference the result correctly (using procedural)?
Try this:
$userid = "admin";
$stmt = mysqli_stmt_init($link);
if (mysqli_stmt_prepare($stmt, 'SELECT * FROM user_info WHERE userid = ?')) {
mysqli_stmt_bind_param($stmt, "s", $userid);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_store_result($stmt);
$number_of_rows = mysqli_stmt_num_rows($stmt);
echo "Number of Rows: $number_of_rows<br />";
$result = mysqli_stmt_get_result($stmt);
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
echo $row["id"];
}
mysqli_stmt_close($stmt);
}
else{
// Catch a database error here
die("Could not query database.");
}
Look at php manual for get-result function: http://php.net/manual/en/mysqli-stmt.get-result.php (Example #2 Procedural style)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.