3

So I have a function to gather user_data in PHP & MYSQL, and the thing is that I want to upgrade MYSQL in MYSQLi.

The MYSQL code is following:

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

The MYSQLi code I tried but with no 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 could be wrong, in the 1:st example I have no errors but the data isn't displaying, and in the 2:nd code I noticed I need the fetch_assoc function to make it work, but here I get the errors saying

Call to a member function fetch_assoc() on a non-object

4

1 回答 1

1

好像您的查询中有错误。MySQli->query()FALSE在失败时返回。

[更新 2] 试试这个代码:

$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);
    }
}
于 2013-01-20T13:57:35.197 回答