はい、問題なく動作するはずです。
ただし、COUNT(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を参照してください(例を使用すると、理解を深めることができます)。
また、必要に応じて、プリペアドステートメントを複数回実行できることにも注意してください。クエリを再実行する前に、新しいパラメータをバインドすることもできます。