Page 1 of 1
How do you Count results for query?
Posted: Mon Apr 15, 2013 8:25 am
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?
Re: How do you Count results for query?
Posted: Mon Apr 15, 2013 12:25 pm
by requinix
Re: How do you Count results for query?
Posted: Mon Apr 15, 2013 12:30 pm
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.
Re: How do you Count results for query?
Posted: Mon Apr 15, 2013 1:10 pm
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).
Re: How do you Count results for query?
Posted: Mon Apr 15, 2013 3:23 pm
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)
{
Re: How do you Count results for query?
Posted: Mon Apr 15, 2013 3:25 pm
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.
Re: How do you Count results for query?
Posted: Mon Apr 15, 2013 4:13 pm
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.
Re: How do you Count results for query?
Posted: Mon Apr 15, 2013 4:22 pm
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.
Re: How do you Count results for query?
Posted: Mon Apr 15, 2013 4:44 pm
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.
Re: How do you Count results for query?
Posted: Mon Apr 15, 2013 4:48 pm
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.
Re: How do you Count results for query?
Posted: Mon Apr 15, 2013 6:41 pm
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?
Re: How do you Count results for query?
Posted: Tue Apr 16, 2013 2:34 am
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??