3

I am getting the error:

Object of class mysqli_result could not be converted to string.

Code:

<?php
  $con=mysqli_connect("78.46.51.231","root","","multicraft_daemon");
  if (mysqli_connect_errno($con)){
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

  $sql = ("select sum(`memory`) from `server`;");

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

  echo $result;    //$result is mysqli_result and can't be forced to string.
?>

What is the correct way to do this?

Dharman
  • 30,962
  • 25
  • 85
  • 135
user2316211
  • 33
  • 1
  • 1
  • 3

2 Answers2

13

You cannot directly output the result of a query. Use:

$sql = ("select sum(`memory`) AS memTotal from `server`");
// Show used memory
$result = mysqli_query($con, $sql);
echo $result->fetch_object()->memTotal;

The $result variable holds an object (of type mysqli_result), from which you can fetch the scalars you need to output.

bwoebi
  • 23,637
  • 5
  • 58
  • 79
3

$result is a result object. From thje manual for mysqli_query():

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.

John Conde
  • 217,595
  • 99
  • 455
  • 496