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:


if (!db) { die('Cannot connect with the database.');}

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

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

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

| improve this question | | | | |
  • 1
    This appears to be a programming issue - if you've connected to the database, then it's certainly not a dba issue. – Vérace Mar 23 '16 at 20:20
  • I'm running this on a CentOS server. The php version is 5.3.3 and I am really confused from all this stuff I read about the mysql connection changes between the different php versions. – Christakitos Mar 23 '16 at 20:33
  • Version of MySQL? Have you tried running the raw SQL code against the database using the MySQL client? Although, SELECT STUDENTNAME FROM students is hardly rocket science - does the table exist? Does it have any records? – Vérace Mar 23 '16 at 20:57
  • Version 5.1.73. Its working fine if i run this with the mysql client, it shows my records. The weird thing that i have done this exactly on localhost and it worked but on this Centos server its dead after the connection – Christakitos Mar 23 '16 at 21:10
  • 1
    Check for errors after each call. That may give you the answer. – Rick James Mar 24 '16 at 5:12

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<br/>", $conn->errno, $conn->error);

Hope it helps.

| improve this answer | | | | |
  • i tried the above too but it seems nothing yet i can't find any error or error message – ANSELEM IKWUEGBU Oct 18 '18 at 17:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.