php mysqli_result 类的对象无法转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21722375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Object of class mysqli_result could not be converted to string in
提问by Chinmay Chandak
I am getting the error:
我收到错误:
Object of class mysqli_result could not be converted to string
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>";
回答by Shankar Damodaran
The mysqli_query()returns an object resource to your $resultvariable, not a string.
在mysqli_query()返回一个对象的资源到你的$result变量,而不是一个字符串。
You need to loop it up and then access the records. You just can't directly use it as your $resultvariable.
您需要循环它,然后访问记录。您不能直接将其用作$result变量。
The code...
编码...
while ($row = $result->fetch_assoc()) {
echo $row['classtype']."<br>";
}
回答by Chandan Singh Gadhwal
Before using the $resultvariable, you should use $row = mysql_fetch_array($result)or mysqli_fetch_assoc()functions.
在使用$result变量之前,您应该使用$row = mysql_fetch_array($result)或mysqli_fetch_assoc()函数。
Like this:
像这样:
$row = mysql_fetch_array($result);
and use the $rowarray as you need.
并$row根据需要使用该数组。
回答by hsz
Try with:
尝试:
$row = mysqli_fetch_assoc($result);
echo "my result <a href='data/" . $row['classtype'] . ".php'>My account</a>";
回答by arturogarrido
Make sure that mysqli_connect()is creating the connection to the DB. You can use mysqli_errno()to check for errors.
确保mysqli_connect()正在创建与数据库的连接。您可以使用mysqli_errno()来检查错误。

