Mysqli_fetch_row() throwing an error when echoing 3 columns instead of 5

hey guys,

I’m not understanding this one. I have this code:
$sql = mysqli_query($conn, "post_date, post_title, post_status FROM " . $dbName . " ORDER BY post_date ASC");

and further down the code, I attempt to echo it out like this:

    while($row = mysqli_fetch_row($sql)) {
        echo '<tr>';
        foreach ($row as $key => $col) {
				echo "<td>$col</td>";
    	}
    		echo '</tr>';
    }

and I’m getting this error: mysqli_fetch_row() expects parameter 1 to be mysqli_result, boolean given

but if I run that same code on data that comes from 5 columns from a different database and table instead of 3 from this db and table, it prints out just fine. Why is this? thanks!

$sql is set to FALSE more early which indicates that your query contains errors.
try to use

if($sql === false) {
    echo mysqli_error($conn);
}

Frank, this code of mine:

$sql = mysqli_query($conn, "post_date, post_title, post_status FROM $dbName ORDER BY post_date ASC");

if($sql === false) {
    echo mysqli_error($conn);
}

if throwing this error when encountering the function you gave me:

You have an error in your SQL syntax; check the manual that corresponds
to your MySQL server version for the right syntax to use near ‘post_date, post_title,
post_status FROM i496696_wp3 ORDER BY post_date ASC’ at line 1

I don’t get this either though. that looks perfectly fine to me. is it? I also tried the sql statement that I included in my first post, but that threw the same error. how simple of a fix is this!? I tend to miss the obvious a lot of times, because I’m more of an expert in complex work. thanks.

The SELECT keyword is a required part of the query.

oh my ! I don’t think I posted the right code then. I’m sure I had the SELECT part in there. I’ll see if I really screwed it up and get back to you guys. sorry about that!

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service