Hi everyone,

I'm having eerors with my php code and here's the errror code that i'm recieving:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /home/w0638385/public_html/blog/index.php on line 9

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in /home/w0638385/public_html/blog/index.php on line 22 Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /home/w0638385/public_html/blog/index.php on line 29
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in /home/w0638385/public_html/blog/index.php on line 41

Here's my php code:

<?php
    ini_set('display_errors',1);
    error_reporting(E_ALL & ~E_NOTICE); 

require("header.php");

$sql= "SELECT entries.*, categories.cat FROM entries, categories WHERE entries.cat_id = categories.id ORDER BY dateposted DESC LIMIT 1;";
$result = mysqli_query($db,$sql);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
echo "<h2 id='title'> <a href='viewentry.php?id=" .$row['id']. "'>" . $row['subject'] . "</a></h2><br />";
echo "<p id='byline'>In <a href='viewcat.php?id=" .$row['cat_id'] . "'>" . $row['cat'] . "</a> - Posted on<span class='datetime'>" . date("D jS F Y g.iA",strtotime($row['dateposted'])) . "</span></p>";
echo "<p>";
echo nl2br($row['$body']);
echo "</p>";

// Comments on this blog entry:

echo "<p id='coments'>";

$commsql = "SELECT name FROM comments WHERE blog_id =" . $row['id'] . " ORDER BY dateposted;";
$commresult = mysqli_query($db, $commsql);
$numrows_comm = mysqli_num_rows($commresult);

// Begin If statement
if ($numrows_comm === 0) {
    echo "(<strong>" .$numrows_comm . "</<strong>) comments : ";
    $i = 1;
}else{
    while ($commrow = mysqli_fetch_array($commresult, MYSQLI_ASSOC)) {
        echo "<a href='viewentry.php?=" . $row['id'] ."#comment" . $i . "'>" .$commrow['name'] . "</a>";

        $i++;
    }
}
echo "</p>";

//  Display the previous 5 blog entries:

$prevsql = "SELECT entries.* ,categories.cat FROM entries, categories WHERE entries.cat_id = categories.id ORDER BY dateposted DESC LIMIT 1, 5;";
$prevresult = mysqli_query($db, $prevsql);
$numrows_prev = mysqli_num_rows($prevresult);

echo "<div id='prev'>";
if($numrows_prev == 0) {
    echo "<p>No previous entries.</p>";
}
else{
    echo "<ul>";
    while ($prevrow = mysqli_fetch_array($prevresult, MYSQLI_ASSOC)) {
        echo "<li> <a href='viewentry.php?id=" . $prevrow['id'] . "'>" . $prevrow['subject'] ."</a></li>";
}
echo "</ul>";
}
echo "</div>";

require("footer.php");
 ?>

What am i doing wrong?

Recommended Answers

All 9 Replies

Member Avatar for Zagga

It looks like all of these errors are caused by a bad query on line 8.
Try adding or die(mysql_error()); to the end of the query to see if an error message gives you more information.

i tried doing that and than all the information went blank except for the header.php and footer.php with the css.

Member Avatar for LastMitch

i tried doing that and than all the information went blank except for the header.php and footer.php with the css.

Apologizes Zagga didn't mean to cut in,

@Its.Captain.Kirk Can you post your table?

Zagga thinks it's your query which is incorrect:

$sql= "SELECT entries.*, categories.cat FROM entries, categories WHERE entries.cat_id = categories.id ORDER BY dateposted DESC LIMIT 1;";

Which I agree with Zagga!

Zagga from UK so you have to be patience. Everyone at Daniweb comes on in different time zones.

I can see a potential problem on line 13 - echo nl2br($row['$body']);, there doesn't seem to be a variable named $body anywhere in the code. Did you mean to put echo nl2br($row['body']); i.e. to reference a field named "body" in your database?

As for the MySQL warnings I would check the return values of each of the statements by doing var_dump(). It seems that your first query is returning a boolean value (Probably false) instead of a result set as you might expect. This means there is probably something wrong with your query.

I just tried doing that to that and it returns NULL

Member Avatar for LastMitch

I just tried doing that to that and it returns NULL

Can you post your table?

That indicates there is something wrong with your initial query if mysqli_query is returning null. You should get into the habit of always checking the return values of functions before proceeding:

$result = mysqli_query($db, @sql);
if ($result) {
    $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
    if ($row) {
        // Use $row here...
    }
}

That way your code won't try to continue executing after an error has occurred and you should be able to pinpoint the problem easier. As LastMitch said, it might be worth posting your table.

I've attached my entries table or do you want the whole entire blog database?

Can you post both the relevant tables, entries and categories? I can't see the full entries table in your PDF either, it cuts off the table at the end of the page. Also, have you tried as Zagga suggested, adding or die(mysqli_error($conn)); (Where $conn is the link to the handle returned by mysqli_connect()) after your mysqli_query() statements? That should hopefully print out a more detailed error message.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.