开发者

Using mysql_fetch_array(); for prepared statements

     if ($q -> num_rows == 1) {
            $q = $dbc -> prepare("UPDATE accounts SET logcount = '0' WHERE email = ?");
            $q -> bind_param('s', ($_SERVER['QUERY_STRING']));
                        $q -> fetch_array(MYSQLI_ASSOC);
            $q -> execute();
            echo '

Congratulations ' . $q['user开发者_开发技巧name'] . ' your account is now active!

'; }

How come when using $q['username'] it doesn't fetch the row username?

I am new to using prepared statements please forgive me :D!

Thanks.


 if ($q -> num_rows == 1) {
        $q = $dbc -> prepare("UPDATE ...");

You are overwriting your $q variable which presumably held the result of a SELECT (guessing).

Use a different variable for the UPDATE part.


You are executing an UPDATE statement. To retrieve data you should use a SELECT statements. UPDATE statements returns only true or false; also mysql function fetch_array() should trow an error if you are trying to fetch a non result.

Since you code is starting with if ($q -> num_rows == 1) {, I think that $q -> fetch_array(MYSQLI_ASSOC); is referring to a previous query. To separate and do not overwrite the two queries you should execute this code instead:

if ($q -> num_rows == 1) {
    $q2 = $dbc -> prepare("UPDATE accounts SET logcount = '0' WHERE email = ?");
    $q2 -> bind_param('s', ($_SERVER['QUERY_STRING']));
        $q -> fetch_array(MYSQLI_ASSOC);
    $q2 -> execute();
    echo 'Congratulations ' . $q['username'] . ' your account is now active!';
}

References:

  • UPDATE statements
  • SELECT statements
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜