簡體   English   中英

PHP mySQLi查詢返回數據但不顯示數據

[英]PHP mySQLi query returning data but not displaying it

美好的一天,我已經對這個問題進行了廣泛的研究,但是不幸的是,沒有任何相關的問題解決了我的問題。

在這里,我有一個非常基本的PHP mySQLi db連接。 連接成功,在表上運行的查詢也將成功。 問題是結果集將不會顯示。 我所有的引用都是正確的,當我檢查是否填充了結果集時,它就是正確的。 我相信問題出在我的while塊上,但是運行時不會返回任何錯誤。 感謝您的時間

<?php
$db = mysqli_connect('localhost','root','','securitour') //connection to the         database
or die('Error connecting to MySQL server.');
?>

<html>
<head>
</head>
<body>
<?php

$query = "SELECT * FROM location"; //The SQL query
mysqli_query($db, $query) or die('Error querying database.'); 
$result = mysqli_query($db, $query); //query the table an store the result set in a     variable
$row = mysqli_fetch_array($result); //create an array and store the records of the     result set in it

if (mysqli_num_rows($result) != 0) //to check if the result set contains data
{
    echo "results found"; //THIS is what is returned.
} 
else 
{
    echo "results not found";
}
while ($row = $result->fetch_assoc()) //itterate through the array and display the         name column of each record
    {
    echo $row['name'];
    }
    mysqli_close($db);
?>
</body>
</html>

您不需要運行mysqli_query()兩次。 並且您需要將mysqli_fetch_assoc用於關聯數組

<?php
$db = mysqli_connect('localhost','root','','securitour') or die('Error connecting to MySQL server.');
?>

<html>
<head>
</head>
<body>
<?php
$query = "SELECT * FROM location"; //The SQL query
$result = mysqli_query($db, $query) or die('Error querying database.'); //query the table an store the result set in a     variable
$row = mysqli_fetch_assoc($result); //create an array and store the records of the     result set in it

if (mysqli_num_rows($result) != 0) //to check if the result set contains data
{
    echo "results found"; //THIS is what is returned.
} else {
    echo "results not found";
}

foreach ( $row as $name=>$val) {
    echo $name . ':' . $val . '<br>';
}
mysqli_close($db);
?>
</body>
</html>

很多事情不就在這里

  1. 您要處理mysqli_query()函數兩次-不需要。

  2. 您正在選擇SQL查詢(SELECT *)中的所有字段。 您應該按名稱選擇字段。

  3. 您正在過程和基於類的MySQLi之間進行交換-您應該堅持一個或另一個。

嘗試以下方法:

    <?php
    $db = mysqli_connect('localhost','root','','securitour') //connection to the         database
    or die('Error connecting to MySQL server.');
    ?>

    <html>
    <head>
    </head>
    <body>
    <?php

    $query = "SELECT name FROM location"; //The SQL query
    $result = mysqli_query($db, $query) or die('Error querying database'); //query the table an store the result set in a     variable
    if(mysqli_num_rows($result) > 0){
        echo "Results found!";
        while($row = mysqli_fetch_array($result)){ //create an array and store the records of the     result set in it
            echo $row['name'];
        }
    } else {
        echo "results not found";
    }
        mysqli_close($db);
    ?>
    </body>
    </html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM