[PHP + MySQL]数据库SELECT查询返回无结果

时间:2015-06-27 13:44:15

标签: php mysql database mysqli

我正在尝试访问我的数据库以获取一些数据,但它会继续返回并出现以下错误。

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\default.php on line 84

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\default.php on line 86

我检查了连接,代码正在正确输入数据,它只是不会返回任何值的结果查询。就个人而言,我无法看到错误的位置,因为其他查询(如INSERT和CREATE)运行正常。

<?php
    mysqli_select_db($conn, $dbName);
    $sql = "SELECT * FROM tbl_users WHERE id = 1;";
    $result = mysqli_query($conn, $sql);
    echo mysqli_num_rows($result); //Line 84

    if (mysqli_num_rows($result) > 0) { //Line 86
        while($row = mysqli_fetch_assoc($result)) {
            ...
        }
    } else {
        echo "0 results";
    }
?>

如果您需要任何进一步的信息,请询问我,我会尝试提供。

完整代码:

//**Create Connection**//
$conn = mysqli_connect($serverName, $username, $password);

//**Check Connection**//
if (!$conn) { die("Connection failed: " . mysqli_connect_error()); }
else { echo "<p>Connected successfully!</p>"; }



//**Create Database**//
$dbName = "myDB";
$sql = "CREATE DATABASE IF NOT EXISTS " . $dbName . " CHARACTER SET utf8 COLLATE utf8_general_ci;";

//Error Handling
if (!mysqli_query($conn, $sql)) { echo "Error creating database: " . mysqli_error($conn); }
else { echo "<p>Database created successfully!</p>"; }

//**Create Table**//
mysqli_select_db($conn, $dbName);
$tbl_name = "tbl_users";
$sql = "CREATE TABLE IF NOT EXISTS " . $tbl_name . " (id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, firstName VARCHAR(64) NOT NULL, lastName VARCHAR(64) NOT NULL, userEmail VARCHAR(256) NOT NULL, reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP) CHARACTER SET utf8 COLLATE utf8_general_ci;";

//Error Handling
if (!mysqli_query($conn, $sql)) { echo "Error creating table: " . mysqli_error($conn); }
else { echo "<p>Table '" . $tbl_name . "' created successfully!</p>"; }

mysqli_select_db($conn, $dbName);
$sql = "SELECT * FROM tbl_users WHERE id = 1";
$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));

if (!$result = mysqli_query($conn, $sql)) {
printf("Errormessage: %s\n", mysqli_error($conn));
}

if (mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) {
        echo $row . "<br>";
    }
} else {
    echo "0 results";
}

5 个答案:

答案 0 :(得分:2)

您的代码没问题。尝试在数据库中执行该SQL查询...

答案 1 :(得分:2)

您没有向我们展示您的所有代码。

您有两个同时通话,并且您收到错误报告

答案 2 :(得分:2)

问题在于:

您在这段代码中使用mysqli_query()两次:

$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));

if (!$result = mysqli_query($conn, $sql)) {
printf("Errormessage: %s\n", mysqli_error($conn));
}

你需要删除一个并执行:

if (!$result) {
printf("Errormessage: %s\n", mysqli_error($conn));
}

同时添加转义路线else{...}affected_rows()

  • 这是您的查询失败的原因。

修改

这个,您使用$conn两次而不使用变量引用来查询:

mysqli_select_db($conn, $dbName);
$sql = "SELECT * FROM tbl_users WHERE id = 1";
$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));

将其更改为并使用mysqli_connect()的4参数方案,因为此时已经创建了DB :(假设已经在上面创建了&#34; 1&#34;的ID)

$dbName = "myDB";
$conn = mysqli_connect($serverName, $username, $password, $dbName);

$sql = "SELECT * FROM tbl_users WHERE id = 1";
$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));

if (!$result) {
printf("Errormessage: %s\n", mysqli_error($conn));
}
else{ echo "Success"; }

或删除or die(mysqli_error($conn))并在之后传递错误,如果有的话。

$dbName = "myDB";
$conn = mysqli_connect($serverName, $username, $password, $dbName);

$sql = "SELECT * FROM tbl_users WHERE id = 1";
$result = mysqli_query($conn, $sql);

if (!$result) {
printf("Errormessage: %s\n", mysqli_error($conn));
}
else{ echo "Success"; }

(附加编辑)

你也可以尝试这种方法,并与我上面已经说过的一起使用:

$numrows = mysqli_num_rows($result);

if($numrows > 0){
// do something
}

error reporting添加到文件的顶部,这有助于查找错误。

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

旁注:错误报告应仅在暂存时完成,而不是生产。

答案 3 :(得分:1)

使用mysqli_error();查看问题所在

    mysqli_free_result($result);
    if (!$result = mysqli_query($conn, $sql)) {
        printf("Errormessage: %s\n", mysqli_error($conn));
   }

如果您的回答是&#34;错误消息:命令不同步;你现在不能运行这个命令&#34;那么我认为你的问题是你仍然在上面的代码之前添加mysqli_free_result($result);的其他查询中连接,以查看真正的错误。

答案 4 :(得分:0)

我不认为您的代码有任何问题,但您的编程风格存在一些问题。你应该尝试一下

   $conn = mysqli_connect('host', 'username', 'password', 'database', '[option port number]') or die('<h1>Error connecting to database server</h1>'); 

    //=> the optional port number defaults to 3306 - recommended if your database is hosted on a live server else leave blank 

    if ($conn) {

    $query = "SELECT * FROM `tbl_users` WHERE `id` = 1"; 

        //if id is unique this should just be a single row from your database 

    $result = $mysqli_query($dbc, $query) or die("Invalid query sytax" . mysqli_error($conn));

        if($result){

            echo "The number of rows are(is) " . mysqli_num_rows($result);

                while($row = mysqli_fetch_assoc($result)) {

                    //do stuff here  or remove while loop and var_dump() 
                }
        } else {

            echo "No record found"; 
        }
    }