简体   繁体   中英

MySQL query returns empty result when used in PHP, but works perfectly in MySQL client

I see there are many other similar posts regarding similar problems out there, but no one of them solved my problem.

I have the following .php file:

<?php
$servername = "localhost";
$username = "myusername";
$password = "mypassword";

// Create connection
$conn = mysqli_connect($servername, $username, $password);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";

$sql = "SELECT DISTINCT fase FROM tornei WHERE categoria='18' AND edizione='4' AND anno='2015'";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        echo "fase: " . $row["fase"];
    }
} else {
    echo "0 results";
}

mysqli_close($conn);
?> 

The query correctly returns the expected result when executed on a MySQL client:

+--------+
| fase   |
+--------+
| gironi |
+--------+

In the logs, I get the following error:

PHP Warning:  mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in /home/admin/domains/domain.com/public_html/query.php on line 18

You have not put the "database" name in mysqli_connect. Correct syntax is here:

$con=mysqli_connect("localhost","my_user","my_password","my_db");

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