mysqli : mysqli_result could not be converted to string

扶醉桌前 提交于 2020-01-09 12:02:00

问题


I know there have been asked lots of similair questions like this one, but I just can't translate it to my problem so thats why I ask again. The code used to work fine but since the mysql is going to be deprecated I wanted to translate to mysqli.

I receive the following error when trying to read something from database: Catchable fatal error: Object of class mysqli_result could not be converted to string. It refers to line 12, which is

 echo $result;

FULL CODE:

$previd ="10";
$query="SELECT * FROM contacts WHERE id='$previd'";
$result = $mysqli->query($query);

echo $result;


$num=$result->num_rows;

$mysqli->close();

echo "<b><center>Database Output</center></b><br><br>";

$i=0;
while ($i < $num) {
$id=mysql_result($result,$i,"id");
$first=mysql_result($result,$i,"first");
$last=mysql_result($result,$i,"last");
$phone=mysql_result($result,$i,"phone");
$mobile=mysql_result($result,$i,"mobile");
$fax=mysql_result($result,$i,"fax");
$email=mysql_result($result,$i,"email");
$web=mysql_result($result,$i,"web");
$content=mysql_result($result,$i,"content");

echo "<u>$id</u><b>$first $last</b><br>Phone: $phone<br>Mobile: $mobile<br>Fax:     $fax<br>E-mail: $email<br>Web: $web<br><hr><br>$content";

$i++;
 }

How can I solve this?


回答1:


$num=$result->num_rows;

echo "<b><center>Database Output</center></b><br><br>";

while ($row = $result->fetch_assoc()) {  

   echo "<u>".$row['id']."</u><b>".$row['first']."</b>"; //etc...
 }

is much easier.




回答2:


That is because mysqli_result is a result set. I don't know what you would expect to gain from echoing it, just as you wouldn't get anything meaningful from echoing the result set of a mysql_query query which uses a SELECT.

You need to use the various class methods to access data in the result set.

http://php.net/manual/en/class.mysqli-result.php



来源:https://stackoverflow.com/questions/14573134/mysqli-mysqli-result-could-not-be-converted-to-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!