警告:mysqli_fetch_array()要求参数1为mysqli_result,[复制]中给出布尔值
This question already has an answer here:
I have a problem here is my page which i getting the error from
<?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);
?>
and the error is showing this line
$row = mysqli_fetch_array($query);
and the second error is in this code
<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>
error line is this
<?php while($comment = mysqli_fetch_array($comment_query)): ?>
pleas help me i am very new to mysqli
</div>
There are a lot of possible reasons. Main reason: The query failed. You may have tried to access a table, a column, that is not existing.
Use die($query)
and try it out in phpMyAdmin and see if it really executes.
Alternatively using mysqli_error($conn_object)
will also contain some information about what happened.
Also, while using the loop, try to give in this format:
while (false != ($comment = mysqli_fetch_array($comment_query))
Snippet
A typical database query, I would write this way:
<?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.";
?>
In mysqli_connect()
you should pass only hostname to its first argument and pass port as an optional fifth parameter. Also, mysqli would have you specify the database in the mysqli_connect()
call instead of having a separate function analogous to mysql_select_db()
.
check here for more info and answer to this.