TheStalker Posted January 8, 2009 Share Posted January 8, 2009 ok i want to count the number of rows in a table and display the total. This is the code i have so far $result = mysql_query("SELECT COUNT(*) FROM table WHERE example ='0'"); $num_rows = mysql_fetch_row($result); $num_rows = $num_rows; echo "There are ".$num_rows." rows."; i seem to be getting the following error Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in Link to comment https://forums.phpfreaks.com/topic/139988-solved-simple-mysql-count-issue/ Share on other sites More sharing options...
GingerRobot Posted January 8, 2009 Share Posted January 8, 2009 Is table your actual table name? If so, it's a reserved word and cannot be used without being escaped with backticks (`) Link to comment https://forums.phpfreaks.com/topic/139988-solved-simple-mysql-count-issue/#findComment-732384 Share on other sites More sharing options...
robb73 Posted January 8, 2009 Share Posted January 8, 2009 change you code to include the following as appropriate: $dbc = mysql_connect( ... ) OR die ('MySQL connect issue: ' . mysql_error() ); mysql_select_db( ... ) OR die ('DB select issue: ' . mysql_error() ) ; $result = mysql_query("SELECT COUNT(*) FROM table WHERE example ='0'") OR die ('SQL issue: ' . mysql_error() ); and see what error message you get back. Also, remove any error suppressor ( @ ) from your db connect and select statements, if you have one. Link to comment https://forums.phpfreaks.com/topic/139988-solved-simple-mysql-count-issue/#findComment-732387 Share on other sites More sharing options...
abdfahim Posted January 8, 2009 Share Posted January 8, 2009 $result = mysql_query("SELECT COUNT(*) as abc FROM table WHERE example ='0'"); $num_rows = mysql_fetch_assoc($result); $num = $num_rows['abc']; echo "There are ".$num." rows."; Link to comment https://forums.phpfreaks.com/topic/139988-solved-simple-mysql-count-issue/#findComment-732409 Share on other sites More sharing options...
TheStalker Posted January 8, 2009 Author Share Posted January 8, 2009 change you code to include the following as appropriate: $dbc = mysql_connect( ... ) OR die ('MySQL connect issue: ' . mysql_error() ); mysql_select_db( ... ) OR die ('DB select issue: ' . mysql_error() ) ; $result = mysql_query("SELECT COUNT(*) FROM table WHERE example ='0'") OR die ('SQL issue: ' . mysql_error() ); and see what error message you get back. Also, remove any error suppressor ( @ ) from your db connect and select statements, if you have one. i got this as an error SQL issue: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'example ='0'' at line 1 Link to comment https://forums.phpfreaks.com/topic/139988-solved-simple-mysql-count-issue/#findComment-732430 Share on other sites More sharing options...
TheStalker Posted January 8, 2009 Author Share Posted January 8, 2009 $result = mysql_query("SELECT COUNT(*) as abc FROM table WHERE example ='0'"); $num_rows = mysql_fetch_assoc($result); $num = $num_rows['abc']; echo "There are ".$num." rows."; Just tried this code and get this error Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in do i need to change the abc to something ? Link to comment https://forums.phpfreaks.com/topic/139988-solved-simple-mysql-count-issue/#findComment-732435 Share on other sites More sharing options...
GingerRobot Posted January 8, 2009 Share Posted January 8, 2009 Is table your actual table name? If so, it's a reserved word and cannot be used without being escaped with backticks (`) Link to comment https://forums.phpfreaks.com/topic/139988-solved-simple-mysql-count-issue/#findComment-732441 Share on other sites More sharing options...
TheStalker Posted January 8, 2009 Author Share Posted January 8, 2009 Is table your actual table name? If so, it's a reserved word and cannot be used without being escaped with backticks (`) no my table is not called table i just used table and example while postting on the forum Link to comment https://forums.phpfreaks.com/topic/139988-solved-simple-mysql-count-issue/#findComment-732442 Share on other sites More sharing options...
GingerRobot Posted January 8, 2009 Share Posted January 8, 2009 Would you care to show us the actual example then? It's a bit difficult to debug a syntax error without your actual code. Link to comment https://forums.phpfreaks.com/topic/139988-solved-simple-mysql-count-issue/#findComment-732445 Share on other sites More sharing options...
TheStalker Posted January 8, 2009 Author Share Posted January 8, 2009 ok sorry i didnt think it would matter. $result = mysql_query("SELECT COUNT(*) FROM messaging WHERE read ='0'"); $num_rows = mysql_fetch_row($result); $num_rows = $num_rows; echo "There are ".$num_rows." rows."; Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource messages.php on line 21 Link to comment https://forums.phpfreaks.com/topic/139988-solved-simple-mysql-count-issue/#findComment-732455 Share on other sites More sharing options...
GingerRobot Posted January 8, 2009 Share Posted January 8, 2009 Read is also a reserved word. Use backticks(`) around it or(better) rename your field. See here for a full list of reserved words. Link to comment https://forums.phpfreaks.com/topic/139988-solved-simple-mysql-count-issue/#findComment-732459 Share on other sites More sharing options...
TheStalker Posted January 8, 2009 Author Share Posted January 8, 2009 opps ill rename it and try again. Thanks for everyones input and help Link to comment https://forums.phpfreaks.com/topic/139988-solved-simple-mysql-count-issue/#findComment-732463 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.