Forum Moderators: coopster

Message Too Old, No Replies

SQL issue and mysl num rows

         

bysonary

11:26 pm on Feb 13, 2007 (gmt 0)

10+ Year Member



Hello, the code below keeps giving me the following error and i cant work out why! can anyone help me please, here is the error:

Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in /home/www/juttuffi/questions/admin/answereditform.php on line 10
There is nothing in the database


$aid = (int)$_GET['aid'];

$query1 = mysql_query("SELECT * FROM tquestions WHERE aid='$aid'");
$query2 = mysql_query("SELECT * FROM tquestions WHERE aid='$aid'");

if (mysql_numrows($query1) == 0)
{
echo "There is nothing in the database";
}
else if (mysql_numrows($query2) == 0)
{
echo "There is nothing in the database";
}
else
{
//echo rows etc.
}
?>

I cant think why these mysql_num_rows() function isnt working can someone help? I would appreciate it an awful lot.

[edited by: bysonary at 11:32 pm (utc) on Feb. 13, 2007]

eelixduppy

11:31 pm on Feb 13, 2007 (gmt 0)



Try something like this to check for query errors:

$query1 = mysql_query("SELECT * FROM tquestions WHERE aid='$aid'") or die([url=http://us3.php.net/manual/en/function.mysql-error.php]mysql_error[/url]());
$query2 = mysql_query("SELECT * FROM tquestions WHERE aid='$aid'") or die(mysql_error());

eelixduppy

11:34 pm on Feb 13, 2007 (gmt 0)



Wow...ok...well, it should be
[url=http://us2.php.net/mysql_num_rows]mysql_num_rows[/url]
NOT
mysql_numrows
. That will cause an error too.

You may also want to validate your integer a little better than just casting it to an int. It would be much better if you actually checked to see if the string contains integers otherwise you may get unexpected results:


$pattern = "/^[1-9][0-9]*$/i";
if(!preg_match($pattern,$aid)) {
echo 'Invalid number!';
exit;
}

[edited by: eelixduppy at 11:39 pm (utc) on Feb. 13, 2007]

bysonary

11:35 pm on Feb 13, 2007 (gmt 0)

10+ Year Member



it says:

Unknown column 'aid' in 'where clause'

however there is a column in tquestions called aid so whats that all about?

eelixduppy

11:41 pm on Feb 13, 2007 (gmt 0)




Unknown column 'aid' in 'where clause'

I'd go back and check that real quick. Maybe the case is off? Sometimes it's a simple spelling mistake ;)

bysonary

1:42 am on Feb 14, 2007 (gmt 0)

10+ Year Member



it was spelling how lame is that

eelixduppy

1:47 am on Feb 14, 2007 (gmt 0)



hehehehe...glad you figured it out ;)

jatar_k

3:03 am on Feb 14, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> how lame is that

you should ask how common it is bysonary

I may just do it once per script, at least, makes me want to hurl myself at something. :)

dreamcatcher

9:03 am on Feb 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I may just do it once per script, at least, makes me want to hurl myself at something.

Me too, all the time. Although I`ve got over the hurling stage now, it was getting too painful. ;)

bysonary

4:17 pm on Feb 14, 2007 (gmt 0)

10+ Year Member



my most famous error is forgetting to put the ";" at the end of lines where it is needed, I find that there may be about 3 or 4 lines in a large script with it missing, its funny because after learning php at a very basic level about 2 years ago and then moving towards mysql stuff in the past month i still make the stupidest errors, its never gonna change i dont think.