简体   繁体   中英

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'] will 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
}                                        

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM