-1

I am quite new in PHP and Mysql and keep getting this error

<?php  
$mysqli = new mysqli("localhost", "root", "root");
$mysqli->select_db("maturita");

$sql=$mysqli->query("SELECT name,description FROM `gallery`");

echo $sql;
?>
Norbert
  • 11
  • 3

2 Answers2

0

mysqli->query returns a mysqli_result object, not a string (the error is pretty descriptive).

To query the results, use the fetch functions.

ex:

while ( $rows = $resource->fetch_assoc() ) {
    echo $rows['field'];
}

(beign $resource the result of your query)

doc: http://php.net/manual/es/mysqli-result.fetch-array.php

0

The manual says (https://secure.php.net/manual/en/mysqli.query.php):

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.

So the error is because the PHP can not convert the mysqli_result object to string in the echo function.

You can change you code to:

<?php  
$mysqli = new mysqli("localhost", "root", "root");
$mysqli->select_db("maturita");

$sql=$mysqli->query("SELECT name,description FROM `gallery`");

print_r($sql->fetch_all());
?>

The result will be the array structure of the result.

The description of mysqli_result and other function can be found here: https://secure.php.net/manual/en/class.mysqli-result.php

Rodolpho Freire
  • 210
  • 2
  • 10