MySQL Question

Selecting Data from SQL Database Returns "1"

This is my first SQL Database. I've been able to successfully connect to my server and database. But, when I use a query to select data from one of the columns in my table, it returns the number "1". Why is this?

<?php

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";

$conn = mysqli_connect($servername, $username, $password, $dbname);

if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT Item FROM Items";
$result = $conn->query($sql);

echo "<h1>Connected successfully<h1>";
echo "<p class='lead'>" + $result + "</p>";

$conn->close();

?>


Here's an image of the table named
Items


enter image description here

This is what shows up on the webpage where it should
echo
the contents in the
item
column:

enter image description here

Answer

You're just echoing the whole object, not individual rows. You need to do something like this to iterate over each row.

if ($result = $conn->query($query)) {

    /* fetch object array */
    while ($row = $result->fetch_row()) {
        echo "<p class='lead'>" . $row[0] . "</p>";
    }

    /* free result set */
    $result->close();
}