Jump to content
Larry Ullman's Book Forums

Recommended Posts

i get this message: Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\AppServ\www\view_current.php on line 73

 

i am not sure if the code is correct but i copied it from the book i was doing so well up until now, could someone give me a hand thanks

 

Kind Regards

 

while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {

$bg = ($bg=='#eeeeee' ? '#ffffff' :

Link to comment
Share on other sites

The error message is telling you that your d/b query that $r references has not been successful. For starters, try running that query directly via phpmyadmin or equivalent and ensure there are no typos, all variables are named as in the table etc.

  • Upvote 2
Link to comment
Share on other sites

I would guess the query is not fine. mysqli_query() returns false when a query fails, and that's why the function is complaining about wanting an array. Instead of looking at the query, switch out your variables with values, and run it in something like phpMyAdmin. I'm sure you'll get an error there.

 

Another good thing to do is to echo out your query before it's run. You'll see if the correct values/etc are there then.

Link to comment
Share on other sites

heres my query...

 

 

$q = "SELECT last_name, first_name,

➝ DATE_FORMAT(registration_date, '%M

➝ %d, %Y') AS dr, user_id FROM users

➝ ORDER BY registration_date ASC

➝ LIMIT $start, $display";

 

when i remove this...

DATE_FORMAT(registration_date, '%M

➝ %d, %Y') AS dr

 

the query loads and doesnt nor show errors,so i am wondering if this capter in the book is using the correct syntax

Link to comment
Share on other sites

By looking at the MySQL manual, I think you need at % char before everything in the date_format function. Try this:

 

DATE_FORMAT(registration_date, '%M %d%, %Y') AS dr

 

The difference is "%," compared to just ",". You might need to add a space in "%d%,", but I'm not sure about that.

 

Edit: I have tested now. It works both with and without the %-char in the query. Might be MySQL version specific, but I'm guessing there's just another small error.

Link to comment
Share on other sites

I would have thought the error in the query was due to the ORDER BY clause. In your query you create an alias for the date_registration by do not use it in the ORDER BY clause. It should be

$q = "SELECT last_name, first_name,
➝ DATE_FORMAT(registration_date, '%M
➝ %d, %Y') AS dr, user_id FROM users
➝ ORDER BY dr ASC
➝ LIMIT $start, $display";

 

Its definitely worth spending time on the debugging chapter - you will save yourself loads of time in the future and learn a few tricks. One quick trick is to insert something like

print_r($r) 

in your code which will give you full information about your query in a more readable format.

  • Upvote 3
Link to comment
Share on other sites

Actually, I'm thinking I was incorrect. I created a blog for someone and originally had a similar query (note my order by is DESC).

$r = mysqli_query($dbc, "SELECT title, excerpt, content, image_file, DATE_FORMAT(date_created,'%D %M %Y') AS pubdate FROM articles ORDER BY pubdate DESC LIMIT 5");

The problem was that the rows were being ordered by day rather than the whole date, which technically makes sense. But I was getting the rows in this order

7th March

7th June

6th May

5th January

3rd April

Then I changed the query to

$r = mysqli_query($dbc, "SELECT title, excerpt, content, image_file, DATE_FORMAT(date_created,'%D %M %Y') AS pubdate FROM articles ORDER BY date_created DESC LIMIT 5");

and the rows returned as follows

7th June

6th May

3rd April

7th March

9th February

 

This thread is a good illustration of where aliases come into their own. In the above query, without an alias I would have had a cumbersome bit of code to access the date field e.g.

$r = mysqli_query($dbc, "SELECT title, excerpt, content, image_file, DATE_FORMAT(date_created,'%D %M %Y') AS pubdate FROM articles ORDER BY date_created DESC LIMIT 5");
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
echo "<h2>$row[title]</h2><p> $row[excerpt]</p><p> $row[DATE_FORMAT(date_created, '%D %M %Y')]</p>";
}

Much easier to code

$row[pubdate]

and much less room for error!

 

Anyway Leo - I hope this helped a little and didn't cause you any confusion.

 

And thanks Antonio - you are correct that the column must be selected to use it for ordering.

  • Upvote 1
Link to comment
Share on other sites

Thanks guys you have been great help, makes a lot more sence now thanks margaux thanks antonio..

 

i think i will go back to the debugging chapter, also how do i print this should i just echo?

print_r($r)

Link to comment
Share on other sites

print_r() prints it automatically.

 

Using aliases in the clauses used to be required by MySQL and cause errors if not done, but that's no longer the case.

 

leo, in most situations, if you're taking a query from the book and it's giving that error, the problem is in the database setup. Not the connection, but the definition and population of the database tables themselves.

  • Upvote 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...