Mysql_fetch_assoc in mysqli in function

So, I have a function to collect user_data

in PHP and MYSQL, and the point is that I want to update MYSQL

in MYSQLi

.

Below is the code MYSQL

:

$data = mysql_fetch_assoc(mysql_query("SELECT $fields FROM members where id = $id"));

      

The code MYSQLi

I've tried but don't use:

$data = $db_connect->query("SELECT $fields FROM ´members´ where id = $id");

      

and

$result = $db_connect->query("SELECT $fields FROM ´members´ where id = $id");
$data = $result->fetch_assoc();

      

I don't know what might be wrong, in example 1: st I have no errors but no data is displayed, and in code 2: I noticed that I need a function fetch_assoc

to make this work, but here I am getting errors saying

Calling member function fetch_assoc () on non-object

+3


source to share


1 answer


It looks like you have an error in your request. MySQli->query()

will return FALSE

on failure.

[UPDATE 2] Try this code:



$result = $db_connect->query("SELECT $fields FROM members where id = $id");

if (!$result) {
    printf("Errormessage: %s\n", $db_connect->error);
}
else {
    while ($data = $result->fetch_assoc()) {
        print_r ($data);
    }
}

      

+1


source







All Articles