警告:mysqli_fetch_row()期望参数1为mysqli_result,给定null

时间:2018-09-07 08:25:41

标签: php

有什么问题,无论我做什么打印都一样?一个帮助将不胜感激。

<?php 

if(isset($_POST['submit'])) {



$username = $_POST['username'];
$password = $_POST['password'];


$connection = mysqli_connect('localhost', 'root', '', 'ayaneshapp');


    if($connection) {

    echo "Logged in!";

    } else {

    die("Sorry! Something went wrong.");

    }



    $query = "SELECT * FROM ayaneshtable ";

    $result = mysqli_query($connection, "SELECT * FROM ayaneshtable");

    if(!$result) {

    die('404 ERROR');

    }

}


?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>

<div class="container">

    <div class="col-sm-6">

        <?php 

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

                echo $row['username'];

            }

        ?>

    </div>


</div>  

</body>
</html>

1 个答案:

答案 0 :(得分:0)

$result为空,因为您的代码未放入if语句中。每次在未设置$_POST['submit']的情况下访问页面时,都会引发此错误。

因此,要解决此问题,您既必须将while循环包装在相同的if语句中,也要确保$result被填充,而不管$_POST['submit']中是否有东西。