是的,它应该工作得很好。
但是,请记住,执行 aCOUNT(primary_key)
通常会提供更好的性能。
所以你上面的查询看起来像
// first, setup your DB-connection
$mysqli = new mysqli('example.com', 'user', '********', 'database');
// Preparing the statement
$stmt = $mysqli->prepare('SELECT COUNT(*) FROM pj_galleries WHERE project = ?');
// binding the parameters
$stmt->bind_param('i', $pjInfo['pj_id']); // 'i' signals an integer
// Executing the query
if ( ! $stmt->execute()) {
trigger_error('The query execution failed; MySQL said ('.$stmt->errno.') '.$stmt->error, E_USER_ERROR);
}
// fetching the results
$col1 = null;
$stmt->bind_result($col1); // you can bind multiple colums in one function call
while ($stmt->fetch()) { // for this query, there will only be one row, but it makes for a more complete example
echo "counted {$col1} records\n";
}
$stmt->close(); // explicitly closing your statements is good practice
要获得更好和更完整的解释,请查看:http ://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php (示例应该让您加快速度)。
另请记住,如果需要,您可以多次执行准备好的语句。您还可以在重新执行查询之前绑定新参数。