问题
I'm learning about prepared statements and trying to work with a query that yields multiple rows of results. Right now, I'm just trying to figure out how to determine the number of rows and then make that number display in the html.
My prepared statement looks like this:
if($stmt = $mysqli -> prepare("SELECT field1, field2, field3 FROM table WHERE id= ?ORDER BY id ASC"))
{
/* Bind parameters, s - string, b - blob, i - int, etc */
$stmt -> bind_param("i", $id);
$stmt -> execute();
/* Bind results */
$stmt -> bind_result($testfield1, $testfield2, $testfield3);
/* Fetch the value */
$stmt -> fetch();
/* Close statement */
$stmt -> close();
}
I understand that I'm supposed to first save the results, then use num_rows
, like this:
$stmt->store_result();
$stmt->num_rows;
However, I'm running and issue with the page bugging out when I put that code in there. I haven't even been able to get to the next step of how to display the number of rows
So, the question is: What am I missing in terms of calculating the number of rows inside the prepared statement, then how would I display it with a <?php echo '# rows: '.$WHATGOESHERE;?>
Thanks!!
回答1:
num_rows
returns the number, you have to store it in a variable.
/*.....other code...*/
$numberofrows = $stmt->num_rows;
/*.....other code...*/
echo '# rows: '.$numberofrows;
So full code should be something like this:
if($stmt = $mysqli -> prepare("SELECT field1, field2, field3 FROM table WHERE id= ? ORDER BY id ASC"))
{
/* Bind parameters, s - string, b - blob, i - int, etc */
$stmt -> bind_param("i", $id);
$stmt -> execute();
/* Bind results */
$stmt -> bind_result($testfield1, $testfield2, $testfield3);
/* Fetch the value */
$stmt -> fetch();
$numberofrows = $stmt->num_rows;
/* Close statement */
$stmt -> close();
}
echo '# rows: '.$numberofrows;
回答2:
If you are only interested in the row count instead of the actual rows of data, here is a complete query block that is fitted with error checkpoints and the recommended COUNT(*)
call in the SELECT clause.
if (!$conn = new mysqli("host", "user", "pass", "db")) {
echo "Database Connection Error: " , $conn->connect_error; // don't show this to the public
} else {
if (!$stmt = $conn->prepare("SELECT COUNT(*) FROM `table` WHERE id= ?")) {
echo "Prepare Syntax Error: " , $conn->error; // don't show this to the public
} else {
if (!$stmt->bind_param("s", $id) // if trouble while binding to ? placeholder
|| !$stmt->execute() // or if trouble while executing
|| !$stmt->bind_result($num_rows) // or if trouble while binding to $num_rows
|| !$stmt->fetch()) { // or if trouble while fetching the one row.
echo "Statement Error: " , $stmt->error; // don't show this to the public
}else{
echo $num_rows;
}
$stmt->close(); // no longer need statement
}
$conn->close(); // no longer need connection
}
Or if you want to know the row count before iterating/processing the rows, one way is to lump the entire resultset (multi-dimensional array) into a variable and call count()
/sizeof()
before iterating.
if (!$conn = new mysqli("host", "user", "pass", "db")) {
echo "Database Connection Error: " , $conn->connect_error;
} else {
if (!$stmt = $conn->prepare("SELECT field1, field2, field3 FROM table WHERE id= ? ORDER BY id ASC")) {
echo "Prepare Syntax Error: " , $conn->error;
} else {
if (!$stmt->bind_param("s", $id)
|| !$stmt->execute()
|| !$result = $stmt->get_result()) {
echo "Statement Error: " , $stmt->error;
}else{
$resultset = $result->fetch_all(MYSQLI_ASSOC);
echo "<div>Num: " , sizeof($resultset) , "</div>";
foreach ($resultset as $row) {
echo "<div>Row: {$row['field1']} & {$row['field2']} & {$row['field3']}</div>"; // do what you like
}
}
$stmt->close();
}
$conn->close();
}
*I have tested both of the above snippets to be successful on my localhost.
回答3:
Check out the example #2 here: PHP.net
Use PDO::query() to issue a SELECT COUNT(*) statement with the same predicates as your intended SELECT statement, then use PDOStatement::fetchColumn() to retrieve the number of rows that will be returned. Your application can then perform the correct action.
来源:https://stackoverflow.com/questions/16726023/prepared-statements-number-of-rows