Object of class mysqli_result could not be convert

2019-01-01 02:27发布

问题:

I am getting the error:

Object of class mysqli_result could not be converted to string

This is my code:

$username2 = htmlentities($_SESSION[\'user\'][\'username\'], ENT_QUOTES, \'UTF-8\');

$con = mysqli_connect(\'localhost\',\'root\',\'\',\'test\');

$result = mysqli_query($con, \"SELECT classtype FROM learn_users
                        WHERE username=\'$username2\';\");

echo \"my result <a href=\'data/$result.php\'>My account</a>\";

回答1:

The mysqli_query() returns an object resource to your $result variable, not a string.

You need to loop it up and then access the records. You just can\'t directly use it as your $result variable.

The code...

while ($row = $result->fetch_assoc()) {
    echo $row[\'classtype\'].\"<br>\";
}


回答2:

Before using the $result variable, you should use $row = mysql_fetch_array($result) or mysqli_fetch_assoc() functions.

Like this:

$row = mysql_fetch_array($result);

and use the $row array as you need.



回答3:

Try with:

$row = mysqli_fetch_assoc($result);
echo \"my result <a href=\'data/\" . $row[\'classtype\'] . \".php\'>My account</a>\";


回答4:

Make sure that mysqli_connect() is creating the connection to the DB. You can use mysqli_errno() to check for errors.



标签: php mysqli