简体   繁体   English

PHP mySQLi查询返回数据但不显示数据

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

Good day, I have done extensive research on this issue but unfortunately none of the related issues solved my problem. 美好的一天,我已经对这个问题进行了广泛的研究,但是不幸的是,没有任何相关的问题解决了我的问题。

Here I have a very basic PHP mySQLi db connection. 在这里,我有一个非常基本的PHP mySQLi db连接。 The connection succeeds and so does the query that is run on the table. 连接成功,在表上运行的查询也将成功。 The issue is that the the result set will not display. 问题是结果集将不会显示。 All of my references are correct and when I check to see if the result set is populated, it is. 我所有的引用都是正确的,当我检查是否填充了结果集时,它就是正确的。 I believe the issue is with my while block but no errors are returned when this is run. 我相信问题出在我的while块上,但是运行时不会返回任何错误。 Thank you for your time 感谢您的时间

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

You don't need to run mysqli_query() twice. 您不需要运行mysqli_query()两次。 and you need to use mysqli_fetch_assoc for associative array 并且您需要将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>

Lots of things not right here . 很多事情不就在这里

  1. You are processing the mysqli_query() function twice - there is no need. 您要处理mysqli_query()函数两次-不需要。

  2. You are selecting all fields in your SQL query (SELECT *). 您正在选择SQL查询(SELECT *)中的所有字段。 You should select fields by name. 您应该按名称选择字段。

  3. You're interchanging between procedure and class-based MySQLi - You should stick to one or the other. 您正在过程和基于类的MySQLi之间进行交换-您应该坚持一个或另一个。

Try this instead: 尝试以下方法:

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