0

I've just started learning about mySQL together with PHP(basics), and I am stuck for 2 days now with this error. I have HTML code :

<html>
<head>
    <title>Title</title>
</head>
<body>
    <?php include 'pehape.php' ?>
<span>Username:</span><?php echo $var ?>
</body>
</html>

I am just trying to get the name of an user from database using this code:

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";



$conn = new mysqli($servername, $username, $password, $dbname);

$var = $conn->query("SELECT Name FROM users WHERE ID=0");

?>

This is what I get as a result in web browser.

Username:
Recoverable fatal error: Object of class mysqli_result could not be converted to string in /storage/ssd3/749/7441749/public_html/index.php on line 7

Now after the result that I am getting is Object but I don't know how to convert it to string using PHP. -Does anyone know how to fix this?To convert Object to string.

I have tried using print_r function and it didn't work out for me.I found very little information about this.Also, this is my database structure if you need it for some information( I guess that might have impact, don't know) : http://prntscr.com/l5xu9n

Dharman
  • 30,962
  • 25
  • 85
  • 135

2 Answers2

1

$var in your code is actually an object of type mysqli_result.

This is actually a handle to a resultset, and you have to unload the results from that handle using some sort of fetch.

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

$conn = new mysqli($servername, $username, $password, $dbname);

// dont normally get ID's of zero so I chnaged this to 1
$result = $conn->query("SELECT Name FROM users WHERE ID=1");

// now get the result row
$row = $result->fetch_assoc();     // row will be an array

?>

Now in your HTML you can do this

<html>
<head>
    <title>Title</title>
</head>
<body>
    <?php include 'pehape.php' ?>
<span>Username:</span><?php echo $row['Name'];?>
</body>
</html>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • It works. One more question,do I have to do this every time I want to retrieve the value from database,or was this for some reason special? – Ognjen Andric Oct 14 '18 at 16:50
  • Every `query()` that involves a `SELECT` will create a resultset. You always have to fetch the rows after the query somehow. – RiggsFolly Oct 15 '18 at 07:47
0

You should fetch your result like this:

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

$conn = new mysqli($servername, $username, $password, $dbname);

if($result = $conn->query("SELECT Name FROM users WHERE ID=0")) {
    $var = $result->fetch_object();
    $result->close();
}
?>

Then in your HTML:

<html>
<head>
    <title>Title</title>
</head>
<body>
    <?php include 'pehape.php' ?>
<span>Username:</span><?php echo $var->Name ?>
</body>
</html>

Check the docs: http://php.net/manual/pt_BR/mysqli-result.fetch-object.php

timarcosdias
  • 385
  • 3
  • 10