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