The following code uses a prepared statement to select some results.
$link = mysqli_connect($conString['server'], $conString['username'], $conString['password'], $databaseName);
if (!mysqli_connect_errno()) {
$query = "SELECT name, active FROM usertable WHERE name=? AND active=?";
$statement = mysqli_stmt_init($link);
if (mysqli_stmt_prepare($statement, $query)) {
mysqli_stmt_bind_param($statement, 'si', $userName, $userActive);
mysqli_stmt_execute($statement);
// mysqli_stmt_store_result($statement); Do I need this?
mysqli_stmt_bind_result($statement, $result1, $result2);
mysqli_stmt_fetch($statement);
if ((!isset($result1)) || (!isset($result2))) {
echo 'OK';
}
// mysqli_stmt_free_result($statement); Do I need this?
mysqli_stmt_close($statement);
}
mysqli_close($link);
}
I have commented out the two lines because I am not sure if I need them?
In old MySQL I would always free the memory, in this case using mysqli_stmt_free_result($statement); But I have read you only do this if you have used mysqli_stmt_store_result($statement); Before hand.
My question is do I need to use either (best coding practice) in a prepared statement, as i haven't seen any examples that do when using mysqli_stmt_bind_result();