Object of class mysqli_result could not be convert

2019-01-01 02:23发布

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>";

标签: php mysqli
4条回答
若你有天会懂
2楼-- · 2019-01-01 02:56

Try with:

$row = mysqli_fetch_assoc($result);
echo "my result <a href='data/" . $row['classtype'] . ".php'>My account</a>";
查看更多
与君花间醉酒
3楼-- · 2019-01-01 02:57

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>";
}
查看更多
听够珍惜
4楼-- · 2019-01-01 03:02

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

查看更多
余生请多指教
5楼-- · 2019-01-01 03:08

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.

查看更多
登录 后发表回答