fetch_array with prepared statement? PHP MYSQL?

走远了吗. 提交于 2020-01-06 05:35:09

问题


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'] will not print? It returns a : Call to undefined method mysqli_stmt::fetch_array()

Thanks.


回答1:


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.




回答2:


You need to call $q -> execute(); prior to fetching result.




回答3:


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
}                                        


来源:https://stackoverflow.com/questions/5679600/fetch-array-with-prepared-statement-php-mysql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!