leo Posted November 1, 2012 Share Posted November 1, 2012 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 More sharing options...
margaux Posted November 1, 2012 Share Posted November 1, 2012 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. 2 Link to comment Share on other sites More sharing options...
leo Posted November 1, 2012 Author Share Posted November 1, 2012 my code seems to look fine and my query, do you think it might have something to do with table names not have index's? Link to comment Share on other sites More sharing options...
HartleySan Posted November 1, 2012 Share Posted November 1, 2012 If the query is fine, then your DB connection is no good. Try to troubleshoot the DB connection and your call to the mysqli_connect function. Link to comment Share on other sites More sharing options...
Antonio Conte Posted November 1, 2012 Share Posted November 1, 2012 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 More sharing options...
leo Posted November 1, 2012 Author Share Posted November 1, 2012 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 More sharing options...
Antonio Conte Posted November 1, 2012 Share Posted November 1, 2012 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 More sharing options...
leo Posted November 1, 2012 Author Share Posted November 1, 2012 i am very new to php, my debugging skills are zero, i need to read a little more on that chapter,my database connection seems to be fine because its working with all the previous exmaples. Link to comment Share on other sites More sharing options...
leo Posted November 1, 2012 Author Share Posted November 1, 2012 thank you antonio that little trick fixed the code, it now runs the query without errors thanks, i will read further and if i get stuck il be sure to leave a feed, now its time for testing! Link to comment Share on other sites More sharing options...
leo Posted November 1, 2012 Author Share Posted November 1, 2012 i was using this code for paginating by the way Link to comment Share on other sites More sharing options...
Antonio Conte Posted November 1, 2012 Share Posted November 1, 2012 Remember that you can edit posts here. There's an edit button in the right bottom corner of your posts. That was kind of a weird error. Did not know you had to do that trick before right now myself. Glad you got it working. Link to comment Share on other sites More sharing options...
margaux Posted November 2, 2012 Share Posted November 2, 2012 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. 3 Link to comment Share on other sites More sharing options...
Antonio Conte Posted November 2, 2012 Share Posted November 2, 2012 Wow, margaux. I did not think of that. You are 100% correct, though. You need to select a column to be able to order by it. Link to comment Share on other sites More sharing options...
margaux Posted November 2, 2012 Share Posted November 2, 2012 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. 1 Link to comment Share on other sites More sharing options...
leo Posted November 3, 2012 Author Share Posted November 3, 2012 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 More sharing options...
Larry Posted November 5, 2012 Share Posted November 5, 2012 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. 1 Link to comment Share on other sites More sharing options...
Recommended Posts