0

I'm trying to echo the result of a mysqli_query however I keep getting the error 'Catchable fatal error: Object of class mysqli_result could not be converted to string' on line 'echo $result;'. Is there any way I can convert it into a string so that it can be echoed? (P.S sorry if this is easy, I'm new to coding.)

My database is successfully connected and the SQL statements definitely work.

$sql= "SELECT ImageURL FROM `unnormalisedtable` WHERE Yeargroup = 9 ORDER BY RAND() LIMIT 1" ;

$result = mysqli_query($db, $sql);

echo $result;

The expected output is that the result of my SQLi query will be printed on screen, however the error is generated instead. Thanks for any help in advance.

Martin
  • 22,212
  • 11
  • 70
  • 132
danizw
  • 3
  • 1
  • 3
  • [Read the manual and look at their examples](https://www.php.net/manual/en/mysqli.query.php) – Martin Apr 09 '19 at 21:20
  • be careful using ` ORDER BY RAND()` its performance death if the table is 'large' –  Apr 09 '19 at 21:33

1 Answers1

1

Since you limit the selection to one entry use

$row = mysqli_fetch_array($result);
echo $row['ImageURL'];

If you select more than one entry loop over the result.

while($row = mysqli_fetch_array($result)) {
      echo $row['ImageURL'];
}
nullpointr
  • 524
  • 4
  • 18