Jump to content

[SOLVED] simple MySql count issue?


TheStalker

Recommended Posts

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

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.

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

$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 ?

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.