TutorMe Posted January 4, 2008 Share Posted January 4, 2008 I went to w3schools, and looked at the MYSQL select page and copied directly from it. The only thing I changed was the database information and what is echoed. Here is the code: <?php $result = mysql_query("SELECT * FROM Categories ORDERBY cat_id ASC"); while($row = mysql_fetch_array($result)) { echo $row['cat_id']; echo "<br />"; } mysql_close($con); ?> It shows this error: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /Applications/xampp/xamppfiles/htdocs/Source/test.php on line 25 I'm assuming it's my test server. I'm using XAMPP on Mac OS X 10.5. The thing is, XAMPP come with a mysql example, and it uses the same code, and it works. Any ideas? Quote Link to comment Share on other sites More sharing options...
revraz Posted January 4, 2008 Share Posted January 4, 2008 ORDERBY should be ORDER BY Quote Link to comment Share on other sites More sharing options...
TutorMe Posted January 4, 2008 Author Share Posted January 4, 2008 Thanks, but the same error is still displayed. ??? Quote Link to comment Share on other sites More sharing options...
revraz Posted January 4, 2008 Share Posted January 4, 2008 Do some error reporting on the query to see where it's failing. Quote Link to comment Share on other sites More sharing options...
TimUSA Posted January 4, 2008 Share Posted January 4, 2008 forgot your back ticks i think <?php $result = mysql_query("SELECT * FROM `Categories` ORDER BY `cat_id` ASC"); while($row = mysql_fetch_array($result)) { echo $row['cat_id']; echo "<br />"; } mysql_close($con); ?> Quote Link to comment Share on other sites More sharing options...
drummer101 Posted January 4, 2008 Share Posted January 4, 2008 <?php $result = mysql_query("SELECT * FROM Categories ORDERBY `cat_id` ASC"); ?> Quote Link to comment Share on other sites More sharing options...
revraz Posted January 4, 2008 Share Posted January 4, 2008 Backticks arent required, unless you are using a reserved word. Quote Link to comment Share on other sites More sharing options...
drummer101 Posted January 4, 2008 Share Posted January 4, 2008 Backticks arent required, unless you are using a reserved word. Is there a section in the manual that lists all those words cause I've found a couple on my own but never bothered to get the entire list. Quote Link to comment Share on other sites More sharing options...
revraz Posted January 4, 2008 Share Posted January 4, 2008 http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html Quote Link to comment Share on other sites More sharing options...
TutorMe Posted January 4, 2008 Author Share Posted January 4, 2008 I tried back-ticks, but still the same error. If it was an error on that line (24), it would stop there and show the error right? Instead of going on to the line where it is showing the error (25)? Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted January 4, 2008 Share Posted January 4, 2008 Use mysql_error to get the error that occurs during your query: change: $result = mysql_query("SELECT * FROM Categories ORDERBY `cat_id` ASC"); to $result = mysql_query("SELECT * FROM `Categories` ORDER BY `cat_id` ASC") or die(mysql_error()); Quote Link to comment Share on other sites More sharing options...
drummer101 Posted January 4, 2008 Share Posted January 4, 2008 is the categories column on your table "Categories" or categories? (uc/lc) Quote Link to comment Share on other sites More sharing options...
revraz Posted January 4, 2008 Share Posted January 4, 2008 If he adds some error reporting, it will tell him where the error is. Quote Link to comment Share on other sites More sharing options...
TutorMe Posted January 4, 2008 Author Share Posted January 4, 2008 The case in the name is correct. What do you mean by error reporting? Could you give me a small example? Quote Link to comment Share on other sites More sharing options...
interpim Posted January 4, 2008 Share Posted January 4, 2008 are you selecting your database in your code? Usually that error occurs when you have the connection to the database but haven't selected it. do it with this code... mysql_select_db($database); Quote Link to comment Share on other sites More sharing options...
drummer101 Posted January 4, 2008 Share Posted January 4, 2008 Interpim: I beleive mysql would throw the Connection Failed, using password NO error without the db selected not the mysql_fetch_array is invalid <?php $result = mysql_query("SELECT * FROM `Categories` ORDER BY `cat_id` ASC") or die(mysql_error()); ?> add the or die(mysql_error()) part to your sql query. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted January 5, 2008 Share Posted January 5, 2008 I went to w3schools, and looked at the MYSQL select page and copied directly from it. The only thing I changed was the database information and what is echoed. Here is the code: <?php $result = mysql_query("SELECT * FROM Categories ORDERBY cat_id ASC"); while($row = mysql_fetch_array($result)) { echo $row['cat_id']; echo "<br />"; } mysql_close($con); ?> It shows this error: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /Applications/xampp/xamppfiles/htdocs/Source/test.php on line 25 I'm assuming it's my test server. I'm using XAMPP on Mac OS X 10.5. The thing is, XAMPP come with a mysql example, and it uses the same code, and it works. Any ideas? Are you connecting to the database in any way? Can you post your entire code? And do the table and column exist in your database? Quote Link to comment Share on other sites More sharing options...
TutorMe Posted January 5, 2008 Author Share Posted January 5, 2008 I'm not sure what the problem was. I combined the code from the config file and placed it all in one page, and it worked. So thank you for your suggestions and ideas. I am just overlooking some stuff a lot today. Thanks again everyone. Quote Link to comment Share on other sites More sharing options...
trq Posted January 5, 2008 Share Posted January 5, 2008 What do you mean by error reporting? Could you give me a small example? You should always check the return status of your queries before trying to use any results they may produce. At minimum, as simple SELECT query should look like. <?php // connect to db. $sql = "SELECT * FROM `Categories` ORDER BY `cat_id` ASC"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_assoc($result)) { echo $row['cat_id'] . "<br />"; } } } ?> Now, depending on what you want to occur when an error is found you could make that script more verbose by using.... [code] <?php // connect to db. $sql = "SELECT * FROM `Categories` ORDER BY `cat_id` ASC"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_assoc($result)) { echo $row['cat_id'] . "<br />"; } } else { echo "No records found"; } } else { echo "An error has occured<br />" . mysql_error() . "<br />$sql"; } ?> [/code] Quote Link to comment 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.