views:

78

answers:

3

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.

+2  A: 

You need to query the database first.

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

Should be:

$queryFP = mysql_query("SELECT * FROM db");
svens
Wow, I should have known that. Thanks.
Nik
A: 

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
A: 

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

Col. Shrapnel
$aID is used to select the row of which needs displayed.
Nik
It actually needs the entire row, not just the number of them, which is why $aID is required.
Nik
@Nik there can be gaps with no ids. `SELECT * FROM db, limit $aID,1` query could do the trick.
Col. Shrapnel
In this very example you don't get any rows. Only number.
Col. Shrapnel
I'm aware, I only posted the necessary code to fix the issue, the next block of code pulls the array of which is required then displays it.
Nik
@Nik in THIS very code you are pulling ALL rows from the table. And you should not. I hope you've got enough brains to understand it
Col. Shrapnel
What should I do then, because this is the only logical way to achieve what the rest of the block does.
Nik
@Nik if you need only ONE random row off the whole table, you need 2 steps: 1. Get the NUMBER of rows. 2. Get ONE random row. First step I posted in my answer and query for the second in the comment.
Col. Shrapnel
Oh, I see, why did you say that I shouldn't use $aID for any database related action?
Nik
@Nik because name of the variable makes me think if `id` database field while you cannot use this random value as id.
Col. Shrapnel
And my main intention was to tell you that you shouldn't request whole table data if you need only number of rows.
Col. Shrapnel