0

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?

Dharman
  • 30,962
  • 25
  • 85
  • 135
Mats de Swart
  • 28
  • 1
  • 1
  • 3

2 Answers2

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.

Green Black
  • 5,037
  • 1
  • 17
  • 29
0

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

Mike Brant
  • 70,514
  • 10
  • 99
  • 103