简体   繁体   中英

PHP MySQL queries return no results

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.

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)

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>";
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM