fetch_array with prepared statement? PHP MYSQL?
For some reason I cannot get this to work for the life of me, I am new to prepared statements!
$q = $dbc -> prepare ("SELECT * FROM accounts WHERE email = ? && logcount = ''");
$q -> bind_param ('s', ($_SERVER['QUERY_STRING']));
$row = $q -> fetch_array(MYSQLI_ASSOC);
$q -> execute();
$q -> store_result();
if ($q -> num_rows == 1) {
$q = $dbc -> prepare("UPDATE accounts SET logcount = '0' WHERE email = ?");
$q -> bind_param('s', ($_SERVER['QUERY_STRING']));
$q -> execute();
echo 'Congratulations ' . $row['username'] . ' your account is now active!
';
}
Any ideas why $row['username'] wi开发者_高级运维ll not print? It returns a : Call to undefined method mysqli_stmt::fetch_array()
Thanks.
You don't need fetch_array
in this case.
If you want to use get the data from the query, you need to use bind_result
and fetch
after calling execute
.
You need to call $q -> execute();
prior to fetching result.
You seem to be doing it wrong, when you call execute it returns a result object, which is what you call your fetch methods on:
$q = $dbc->prepare ("SELECT * FROM accounts WHERE email = ? && logcount = ''");
$q->bind_param ('s', ($_SERVER['QUERY_STRING']));
//Updated Here
$result = $q -> execute();
if ($result->num_rows == 1)
{
$assocData = $result->fetch_array(MYSQLI_ASSOC);
//Do other stuff
}
精彩评论