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

PHP mySQLi查询返回数据但不显示数据,php,mysqli,Php,Mysqli,你好, 我对这个问题做了广泛的研究,但不幸的是,没有一个相关的问题解决了我的问题 这里我有一个非常基本的PHP-mySQLi-db连接。连接成功,表上运行的查询也成功。问题是结果集将不会显示。我所有的引用都是正确的,当我检查结果集是否已填充时,它是正确的。我相信问题出在我的while块上,但运行时不会返回任何错误。 谢谢你抽出时间 <?php $db = mysqli_connect('localhost','root','','securitour') //connection to t

你好, 我对这个问题做了广泛的研究,但不幸的是,没有一个相关的问题解决了我的问题

这里我有一个非常基本的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\u query()
两次。对于关联数组,您需要使用
mysqli\u fetch\u 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>

您不需要运行
mysqli\u query()
两次。对于关联数组,您需要使用
mysqli\u fetch\u 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>

很多东西不在这里

  • 您正在处理mysqli_query()函数两次-没有必要

  • 您正在选择SQL查询中的所有字段(选择*)。您应该按名称选择字段

  • 您正在过程和基于类的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>
    

    很多东西不在这里

  • 您正在处理mysqli_query()函数两次-没有必要

  • 您正在选择SQL查询中的所有字段(选择*)。您应该按名称选择字段

  • 您正在过程和基于类的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>
    
    
    

    mysqli\u查询($db$query)another@MasivuyeCokile是的,先生,这是在检查$result中存储的结果集中的行数是否不等于0。如果要在mysqli的过程版本和基于类的版本之间切换,请坚持其中一个版本,最好是基于类的。然后您获取一行。。。在进入while循环之前,不要对其进行任何操作。因此,您将松开第1行completely@RiggsFolly,第一个是检查查询是否成功。但这不是问题所在。我这样做是为了隔离代码,看看出了什么问题。你为什么要在一次之后做两次
    mysqli\u查询($db,$query)
    another@MasivuyeCokile是的,先生,这是在检查$result中存储的结果集中的行数是否不等于0您正在mysqli的过程版本和基于类的版本之间切换,坚持使用其中一种,最好是基于类的。然后您将获取一行。。。在进入while循环之前,不要对其进行任何操作。因此,您将松开第1行completely@RiggsFolly,第一个是检查查询是否成功。但这不是问题所在。我这样做是为了隔离代码,看看哪里出了问题。我认为您的While循环应该是
    $row
    array@RiggsFolly您是对的,尽管循环也在工作。@RiggsFolly很好。这都是关于结果显示的,谢谢:)我认为你的While循环应该是
    $行上的foreach循环array@RiggsFolly您是对的,尽管循环也在工作。@RiggsFolly很好。这一切都是关于结果显示的,谢谢:)re:myskii_query-最初我没有运行它两次,但当发生此错误时,我尝试隔离代码以尝试锁定问题,我显然没有正确执行此操作。该sql查询仅用于测试目的。非常感谢你的回答,这正是我所需要的。没问题,很乐意帮忙。如果答案解决了您的问题,请不要忘记将其标记为正确:Dre:myskii_query-最初我没有运行它两次,但当发生此错误时,我尝试隔离代码以尝试锁定问题,我显然没有正确执行此操作。该sql查询仅用于测试目的。非常感谢你的回答,这正是我所需要的。没问题,很乐意帮忙。如果答案解决了你的问题,别忘了把它标为正确答案:D