FamousMortimer Posted July 9, 2006 Share Posted July 9, 2006 I am having a similar problem with a while loop.....I am trying to have a table where the rows alternate colors, so it becomes easier to read.here is my code:[code] // Make the query. $query = "SELECT last_name, first_name, DATE_FORMAT (registration_date, '%M %d, %Y') AS dr, user_id FROM users ORDER BY $order_by LIMIT $start, $display"; $result = mysql_query ($query); // Run the query.... // Fetch and print all the records. $bg = '#eeeeee'; // Set the background color. while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee'); // Switch the background color. echo '<tr bgcolor="' . $bg . '"> <td align="left"><a href="edit_user.php?id=' . $row['user_id'] . '">Edit</a></td> <td align="left"><a href="delete_user.php?id=' . $row['user_id'] . '">Delete</a></td> <td align="left">' . $row['last_name'] . '</td> <td align="left">' . $row['first_name'] . '</td> <td align="left">' . $row['dr'] . '</td> </tr> '; }[/code]here is the error(warning) I keep getting:[code]Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Apache2\htdocs\view_users.php on line 107[/code]Does anyone have any ideas as to why result becomes invalid? Quote Link to comment https://forums.phpfreaks.com/topic/14134-mysql-query-while-loop-problem/ Share on other sites More sharing options...
kenrbnsn Posted July 9, 2006 Share Posted July 9, 2006 Change this line:[code]<?php $result = mysql_query ($query); ?>[/code]to[code]<?php $result = mysql_query ($query) or die("Problem with the query: $query<br>" . mysql_error()); ?>[/code]and see what the error message tells you.Ken Quote Link to comment https://forums.phpfreaks.com/topic/14134-mysql-query-while-loop-problem/#findComment-55321 Share on other sites More sharing options...
toplay Posted July 10, 2006 Share Posted July 10, 2006 Please don't post your problem in another members topic. I've seen this become very confusing for everyone. As you can see I've split yours into a separate topic.This is a frequent error and is explained in the FAQ topic pinned at the top of this PHP Help area. Here's a quote from it:[quote=http://www.phpfreaks.com/forums/index.php/topic,31047.msg153359.html#msg153359]...these messages are usually attributed to wrong SQL syntax in the query (or not connected to the database). What I mean by wrong syntax includes the possibility of using the wrong table or column names, and not just syntactically incorrect formatting of the query. This message can be avoided altogether; it masks/hides the real error at the query and is a by-product of wrong logic. These messages occur because error checking wasn't performed after a query and a subsequent SQL command was issued to work on the query result (when there was already a query error). This goes back to what I was explaining in the MySQL Data Retrieval section above (which has examples of correct MySQL error checking). You must check for errors and when there are errors, don't execute any more SQL commands that retrieve data or require the use of the results table. Handle the error after a query appropriately and your code logic shouldn't get as far as trying to read or examine the data inappropriately, then this warning message won't ever occur again. You need to display the real error that caused the query to fail in order for you to solve the main problem (which is usually bad SQL query syntax). Not handling errors like this is a serious logic flaw in code and I see it all the time. A big part of coding is really error checking. Don't get lazy or forget to check for errors.[/quote]Do what kenrbnsn has suggested to help yourself see what the query error is. Quote Link to comment https://forums.phpfreaks.com/topic/14134-mysql-query-while-loop-problem/#findComment-55356 Share on other sites More sharing options...
FamousMortimer Posted July 10, 2006 Author Share Posted July 10, 2006 Hey thanks for the advice. I put in the die() function and found out that the problem was in my query, a space in between a the mysql function and the parenthesis {i.e.: 'DATE_FORMAT^(' 'instead of DATE_FORMAT('}Also, I'm sorry for the "breaking the forum rule", thanks for informing me about the confusing practice, I had no idea it was that big of a deal. I appologize.FM Quote Link to comment https://forums.phpfreaks.com/topic/14134-mysql-query-while-loop-problem/#findComment-55384 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.