4

Alright, PHP is throwing this error at me (in the log) when I run the code mentioned below:

Error

mysql_num_rows() expects parameter 1 to be resource, string given in (place) on line 10

Line 9-11

$queryFP = ("SELECT * FROM db");
$countFP = mysql_num_rows($queryFP);
$aID = rand(1, $countFP);

I think it has something to do with the $queryFP's syntax, but I'm not completely sure how to fix it since $queryFP's syntax is the simplest query I've ever seen.

Nik
  • 2,424
  • 3
  • 18
  • 16
  • +1 for the perfectly formed question. Both problem code and error message. Everyone to follow this example. – Your Common Sense May 30 '10 at 12:48
  • possible duplicate of [Warning: mysql_fetch_* expects parameter 1 to be resource, boolean given error](http://stackoverflow.com/questions/11674312/warning-mysql-fetch-expects-parameter-1-to-be-resource-boolean-given-error) – John Conde Jul 28 '12 at 16:47
  • possible duplicate of [mysql\_fetch\_array() expects parameter 1 to be resource, boolean given in select](http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-boolean-given-in-select) – Glavić Jan 07 '14 at 22:26

3 Answers3

3

You need to query the database first.

$queryFP = ("SELECT * FROM db");

Should be:

$queryFP = mysql_query("SELECT * FROM db");
svens
  • 11,438
  • 6
  • 36
  • 55
0

You are missing the mysql_query function, it should be like this:

$queryFP = "SELECT * FROM table_name_here";
$queryFP = mysql_query($queryFP) or die(mysql_error());
$countFP = mysql_num_rows($queryFP);
$aID = rand(1, $countFP);
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
0

As it been said, you're missing mysql_query function.
Though whole approach is wrong. You shouldn't select whole load of ata if you need only number of rows.
So, it must be

$sql = "SELECT count(*) FROM db";
$res = mysql_query($sql) or trigger_error(mysql_error().$sql);
$row = mysql_fetch_row($res);
$countFP = $row[0];
$aID = rand(1, $countFP);

And I hope you won't use $aID for any database related action

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345