0

My queries to MySQL via PHP are returning no results. First, I have tried connecting and doing a select on a known table and get no results. I then try to get a listing of the tables and again no results. When I look at the database via phpMyAdmin I can see the tables and their contents. Here is my code. Can anyone offer some help as to what I am doing wrong?

<?php
# /* $ php -f db-connect-test.php */

echo"preparing to connect";

$dbname = '#########';
$dbuser = '#########';
$dbpass = '#########'; 
$dbhost = 'localhost';

$connect = @mysqli_connect($dbhost, $dbuser, $dbpass, $dbname) or die("Unable to Connect to '$dbhost'");

echo"<html>";
echo"<title>test page</title>";
echo"<body>";
echo"<h2> test page</h2>";

/* check connection */
if ($conn->connect_error) {
    die("Connection failed: " . mysqli_connect_error());
}
else{
     echo"Successfully Connected <p>";
}
if(mysqli_ping($connection)){
    echo "got it<p>";
}
$sql = "SELECT * FROM `announcements`";
$result = mysqli_query($dbname, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
       echo 'date: ' . $row['date'] . '\tTitle: ' . $row['title'] . '\tBody: ' . $row['body'] .'<br />';
    }
} else {
    echo "0 results<p>";
    $sql = "SHOW TABLES";
    $result = mysqli_query($dbname, $sql);

    if (!$result) {
        echo "DB Error, could not list tables<p>";
        echo 'MySQL Error: ' . mysqli_error();
    }
    else{
        while ($row = mysqli_fetch_row($result)) {
            echo "Table: {$row[0]}<p>";
        }
    }

}
$conn->close();

echo"</body>";
echo"</html>";
?>

Here is the result I am seeing:

preparing to connect

test page

Successfully Connected

0 results

DB Error, could not list tables

MySQL Error:

end of results

For some reason I am unable to get MySQL to return a error message.

2
  • @ggorlen SHOW TABLES should have worked right?. Commented Jul 30, 2018 at 5:44
  • Don't suppress the mysqli_connect() function. Suppressing will lead to missing information about warnings and errors. Commented Jul 30, 2018 at 6:16

2 Answers 2

1

When calling mysqli_query()

mysqli_query($dbname, $sql);

The first parameter is your database link not the name...

mysqli_query($connect, $sql);

Also - don't use @ for your connection (or preferably anywhere) as this suppresses errors.

Update: Also just noticed...

mysqli_ping($connection)

which should be...

mysqli_ping($connect)
Sign up to request clarification or add additional context in comments.

Comments

1

You just have to Copy and Paste this code

You don't have to use $dbname Have to use $connect

<?php
# /* $ php -f db-connect-test.php */

echo"preparing to connect";

$dbname = '#########';
$dbuser = '#########';
$dbpass = '#########'; 
$dbhost = 'localhost';

$connect = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname) or die("Unable to Connect to '$dbhost'");

echo"<html>";
echo"<title>test page</title>";
echo"<body>";
echo"<h2> test page</h2>";

/* check connection */
if ($conn->connect_error) {
    die("Connection failed: " . mysqli_connect_error());
}
else{
     echo"Successfully Connected <p>";
}
if(mysqli_ping($connection)){
    echo "got it<p>";
}
$sql = "SELECT * FROM `announcements`";
$result = mysqli_query($connect, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
       echo 'date: ' . $row['date'] . '\tTitle: ' . $row['title'] . '\tBody: ' . $row['body'] .'<br />';
    }
} else {
    echo "0 results<p>";
    $sql = "SHOW TABLES";
    $result = mysqli_query($connect, $sql);

    if (!$result) {
        echo "DB Error, could not list tables<p>";
        echo 'MySQL Error: ' . mysqli_error();
    }
    else{
        while ($row = mysqli_fetch_row($result)) {
            echo "Table: {$row[0]}<p>";
        }
    }

}
$conn->close();

echo"</body>";
echo"</html>";
?>

1 Comment

Thank you very much for your response. This answer and the one above pointed me in the right direction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.