Connected to MySQL database but cannot print the data I connected successfully to my database and the I tried to print my data but I can only see the "Connected succesfully" line in the browser. Here is my code:

&lt;?php 

$db=@mysqli_connect("host","root","pass");
if (!db) { die('Cannot connect with the database.');}

// Check connection
if ($db-&gt;connect_error) {
    die("Connection failed: " . $conn-&gt;connect_error);
}
echo "Connected successfully";

$sql = "SELECT username FROM students";
$result = $conn-&gt;query($sql);

if ($result-&gt;num_rows &gt; 0) {
    // output data of each row
    while($row = $result-&gt;fetch_assoc()) {
        echo "username: " . $row["username"].  "&lt;br&gt;";
    }
} else {
    echo "0 results";
}
$conn-&gt;close();



?&gt;
You definitely have to check your query execution response, as clearly suggested by the mysqli::query documentation, by adding the following code fragment just after the query call.

if (!$result) {
    printf("Error: [%s] %s&lt;br/&gt;", $conn-&gt;errno, $conn-&gt;error);
}


Hope it helps.