警告:mysqli_fetch_array()期望参数1为mysqli_result,布尔值为

时间:2015-12-08 10:12:47

标签: php database mysqli

我有一个问题是我的页面,我从

收到错误
<?php
        // retrive post
        include('config.php');
        include ('function.php');
        dbConnect();

        $query = mysqli_query($GLOBALS["___mysqli_ston"], 
            'SELECT *
            FROM post
            WHERE post_id = 1');
        $row = mysqli_fetch_array($query);
    ?>

并且错误显示此行

$row = mysqli_fetch_array($query);

,第二个错误出现在此代码中

<div class="comment-block">
         <?php while($comment = mysqli_fetch_array($comment_query)): ?>
            <div class="comment-item">
                <div class="comment-avatar">
                    <img src="<?php echo avatar($comment['mail']) ?>" alt="avatar">
                </div>
                <div class="comment-post">
                    <h3><?php echo $comment['name'] ?> <span>said....</span></h3>
                    <p><?php echo $comment['comment']?></p>
                </div>
            </div>
        <?php endwhile?> 
        </div>

错误行是这个

 <?php while($comment = mysqli_fetch_array($comment_query)): ?>     
请求帮助我,我是mysqli的新手

2 个答案:

答案 0 :(得分:0)

有很多可能的原因。主要原因:查询失败。您可能试图访问不存在的表格,列。

使用die($query)并在phpMyAdmin中试用它,看它是否真的执行。

或者,使用mysqli_error($conn_object)也会包含有关所发生事件的一些信息。

此外,在使用循环时,请尝试使用以下格式:

while (false != ($comment = mysqli_fetch_array($comment_query))

<强>段

典型的数据库查询,我会这样写:

<?php
    // Connect to the MySQL Server
    $conn = mysqli_connect("localhost", "root", "password", "database") or die("Cannot Connect to MySQL Server. Error: " . mysqli_connect_error());

    // Once connection is established, build a query
    $query = "SELECT * FROM `users`";
    // Execute the query and store it in the results
    $results = mysqli_query($conn, $query);

    // Only continue if there are rows in your result.
    if (mysqli_num_rows($results))
        // Loop through the results, but don't assign directly inside while
        while (false != ($data = mysqli_fetch_array($results)))
            // Display data using the column name
            echo "User: " . $data["User"];
    // Handle the empty scenario
    else
        echo "No users found.";
?>

答案 1 :(得分:0)

mysqli_connect()中,您应该只将主机名传递给它的第一个参数,并将端口作为可选的第五个参数传递。此外,mysqli会让您在mysqli_connect()调用中指定数据库,而不是具有类似于mysql_select_db()的单独函数。

检查here以获取更多信息并回答此问题。