PHP database access simple example

I have the following mysql query in php:

<?php
$con=mysqli_connect("localhost","root","password", "my_db");
//Check connection
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$query = "SELECT Balance FROM my_table ORDER BY ID DESC LIMIT 1";
$result = mysqli_query($con, $query);

echo $result;
?>

The echo $result does not work and I get a catchable fatal error that mysqli_query cannot be converted into string.

How do I echo my query result?

I believe that is because result is an object and it is expecting a string. Try looking into the fetch command. I believe it would go something like

while($row = mysql_fetch_assoc($result)){
      $stringTest = $row['balance'];
      echo $stringTest;
}

Something like that!

from here

Leave a Reply