How do you Count results for query?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

How do you Count results for query?

Post by simonmlewis »

Code: Select all

$resultrccheck = mysql_query ("SELECT rcstock, photoprimary, price, title FROM products WHEREcode = '$codetick' AND stock = 'in stock'");
$num_rcstock = mysql_num_rows($resultrccheck); 
if ($num_rcstock == 1)
{
I keep getting errors on this second line:

Code: Select all

$num_rcstock = mysql_num_rows($resultrccheck); 
I'm told there is another way, but not found one that doesn't product errors.
[15-Apr-2013 07:22:54] PHP Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/site/public_html/includes/product.inc on line 1267
This is the error. It's pointing at that second line.

Over the weekend when this script would have been run thousands of times, I've only had 68 errors.... but I want none. (don't we all).
So is there a more "up to date" way to do this please?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How do you Count results for query?

Post by requinix »

Code: Select all

WHEREcode
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you Count results for query?

Post by simonmlewis »

It isn't like that on the page. Has a space. Sorry. Or it would thro up more than the error I am getting.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How do you Count results for query?

Post by requinix »

Actually it would raise that exact error.

Okay, so the query you posted isn't a copy/paste of the query you actually have. What is the query you actually have?

Or if you want to continue investigating, use mysql_error to find out why the query failed (which is what happened).
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you Count results for query?

Post by simonmlewis »

Code: Select all

$resultrccheck = mysql_query ("SELECT rcstock, photoprimary, price, title FROM products WHERE romancode = '$romancodetick' AND rcstock = 'in stock'");
            $num_rcstock = mysql_num_rows($resultrccheck); 
            if ($num_rcstock == 1)
{
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you Count results for query?

Post by simonmlewis »

Code: Select all

$resultrccheck = mysql_query ("SELECT rcstock, photoprimary, price, title FROM products WHERE romancode = '$romancodetick' AND rcstock = 'in stock'") or die (mysql_error());
            $num_rcstock = mysql_num_rows($resultrccheck); 
            if ($num_rcstock == 1)
{
No errors shown on screen. I did hear that

Code: Select all

 $num_rcstock = mysql_num_rows($resultrccheck); 
Is not an entirely correct way to count rows before extracting the data.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How do you Count results for query?

Post by requinix »

Given that code I am quite sure it's impossible to get the error you're receiving. On that exact line with the mysql_num_rows() you're getting a "not a valid MySQL result resource" message? And you definitely edited the right file and made sure the changes were applied (saved, uploaded, whatever) when you added the "or die" stuff?
simonmlewis wrote:I did hear that

Code: Select all

 $num_rcstock = mysql_num_rows($resultrccheck); 
Is not an entirely correct way to count rows before extracting the data.
It's correct. Behind the scenes the mysql extension extracted all the data already - when you mysql_fetch_*() you're actually reading from what the extension stashed away.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you Count results for query?

Post by simonmlewis »

That is an exact copy/paste of the script.

And as I say, the error is:
[text][12-Apr-2013 18:48:26] PHP Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/site/public_html/includes/product.inc on line 1267[/text]

So it's clearly on that line.

I don't know what else to say or do.
It's not an error anyone sees or is causing user issues, but I don't like those errors in our error log.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: How do you Count results for query?

Post by McInfo »

simonmlewis wrote:It's not an error anyone sees or is causing user issues, but I don't like those errors in our error log.
Does that mean that you discovered the error by examining the error log rather than seeing it when running the script yourself? If the error doesn't occur when you (specifically) run the script, it may mean that someone was probing for SQL injection vulnerabilities through the $romancodetick variable.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you Count results for query?

Post by simonmlewis »

No, because its not a script that can be triggered in that way. Only if we have assigned a tick.

But yes, only in error log.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: How do you Count results for query?

Post by McInfo »

simonmlewis wrote:No, because its not a script that can be triggered in that way.
How does the script get triggered? How do you assign a tick? How do you guarantee that a tick is what you think it should be?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you Count results for query?

Post by simonmlewis »

Firstly it checks further up if there is an entry in the field "titletick1", along with a few other fields that are relevant to specific products.

If that is true, then it does a count, starting with "romancount1", and keeps counting up until 12.
romancodetick1,2, 3 etc may all have content in their fields.

So this

Code: Select all

$resultrccheck = mysql_query ("SELECT rcstock, photoprimary, price, title FROM products WHERE romancode = '$romancodetick' AND rcstock = 'in stock'") or die (mysql_error());
            $num_rcstock = mysql_num_rows($resultrccheck); 
            if ($num_rcstock == 1)
{
.... checks if the item in romancodetick1 field, if that product is classed as in stock, and if it is, then it does other stuff.

So it's a multi level "if" statement. And only after a few checks, does it then reach that ' "mysql_num_rows".
It doesn't happen regularly at all, considering how many thousands of hits each day the site getes (10s of 1000s).

But it's still a concern. I dont' know if there is a means to pinpoint which product is cause it to error??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply