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? Link to comment https://forums.phpfreaks.com/topic/84542-solved-omg-mysql_fetch_array-wont-work/ Share on other sites More sharing options...
revraz Posted January 4, 2008 Share Posted January 4, 2008 ORDERBY should be ORDER BY Link to comment https://forums.phpfreaks.com/topic/84542-solved-omg-mysql_fetch_array-wont-work/#findComment-430727 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. ??? Link to comment https://forums.phpfreaks.com/topic/84542-solved-omg-mysql_fetch_array-wont-work/#findComment-430730 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. Link to comment https://forums.phpfreaks.com/topic/84542-solved-omg-mysql_fetch_array-wont-work/#findComment-430734 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); ?> Link to comment https://forums.phpfreaks.com/topic/84542-solved-omg-mysql_fetch_array-wont-work/#findComment-430735 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"); ?> Link to comment https://forums.phpfreaks.com/topic/84542-solved-omg-mysql_fetch_array-wont-work/#findComment-430737 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. Link to comment https://forums.phpfreaks.com/topic/84542-solved-omg-mysql_fetch_array-wont-work/#findComment-430738 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. Link to comment https://forums.phpfreaks.com/topic/84542-solved-omg-mysql_fetch_array-wont-work/#findComment-430747 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 Link to comment https://forums.phpfreaks.com/topic/84542-solved-omg-mysql_fetch_array-wont-work/#findComment-430752 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)? Link to comment https://forums.phpfreaks.com/topic/84542-solved-omg-mysql_fetch_array-wont-work/#findComment-430761 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()); Link to comment https://forums.phpfreaks.com/topic/84542-solved-omg-mysql_fetch_array-wont-work/#findComment-430763 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) Link to comment https://forums.phpfreaks.com/topic/84542-solved-omg-mysql_fetch_array-wont-work/#findComment-430786 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. Link to comment https://forums.phpfreaks.com/topic/84542-solved-omg-mysql_fetch_array-wont-work/#findComment-430787 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? Link to comment https://forums.phpfreaks.com/topic/84542-solved-omg-mysql_fetch_array-wont-work/#findComment-430806 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); Link to comment https://forums.phpfreaks.com/topic/84542-solved-omg-mysql_fetch_array-wont-work/#findComment-430807 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. Link to comment https://forums.phpfreaks.com/topic/84542-solved-omg-mysql_fetch_array-wont-work/#findComment-430813 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? Link to comment https://forums.phpfreaks.com/topic/84542-solved-omg-mysql_fetch_array-wont-work/#findComment-430839 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. Link to comment https://forums.phpfreaks.com/topic/84542-solved-omg-mysql_fetch_array-wont-work/#findComment-430937 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] Link to comment https://forums.phpfreaks.com/topic/84542-solved-omg-mysql_fetch_array-wont-work/#findComment-431021 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.