Jump to content

num rows or count


chrispos

Recommended Posts

I am using php 5 and MySQL 5. The idea is to set up stats for customers to see how many times the individuals article has been visited each month. When the page is visited It puts into a table called stats with the id code month and year.I have a chart script but I am having problems with the following script.

 

$link = mysql_connect("mysqlblah", "blah", "blah") or die (mysql_error());
mysql_select_db("blah", $link);

$result = mysql_query("SELECT * FROM `customers`WHERE `sid` = '$sid'", $link);
$num_rows1 = mysql_num_rows($result);
echo "$num_rows1";

 

This works with no problem sid being the customer id. When I add

 

$result = mysql_query("SELECT * FROM `customers`WHERE `sid` = '$sid'And `month` = '$m' And `year` = '$y'", $link);
$num_rows1 = mysql_num_rows($result);
echo "$num_rows1";

 

I get a blank page. So I am trying to get the number of people from the stats table for every month of the year.

 

Any help would be great thanks and have a good day.

Link to comment
https://forums.phpfreaks.com/topic/209002-num-rows-or-count/
Share on other sites

for debug purpose, it would be better if you echo a mysql_error() ...  like this

 

[...]
if ( !is_resource($result) ){

    echo "DEBUG: " . mysql_error() . "<br />";
    
}

[...]

 

otherwise, without other informations, for us is difficult help you

 

 

bye

 

Alberto

 

 

Link to comment
https://forums.phpfreaks.com/topic/209002-num-rows-or-count/#findComment-1091677
Share on other sites

If all you are doing is getting the number of entries, COUNT() is much more efficient.

 

You should explicitly list the fields you want to select in the query, rather than use the * wildcard (unless you actually need the contents of every field).

 

To debug DB query problems, you should be able to echo the query, and display any errors. You can 't echo the query when the query string is part of the query execution. Separate the two, and echo the query with any errors.

 

$query = "SELECT COUNT(`pk_id_field`) FROM `customers` WHERE `sid` = '$sid' AND `month` = '$m' AND `year` = '$y'"'
$result = mysql_query($query, $link) or die( '<br />Query string: ' . $query . <br />Produced error: ' . mysql_error() . '<br />');
$count = mysql_result($result);
echo "$count";

Link to comment
https://forums.phpfreaks.com/topic/209002-num-rows-or-count/#findComment-1091699
Share on other sites

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.