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
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.

Link to comment
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.

 

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
Share on other sites

$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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.