MySQL and PHP Parameter 1 as a Resource

Well, PHP throws this error at me (in the log) when I run the code mentioned below:

Error

mysql_num_rows () expects parameter 1 to be a resource, the line specified in (place) on line 10

Line 9-11

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

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

+4
source share
3 answers

You need to query the database first.

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

Must be:

 $queryFP = mysql_query("SELECT * FROM db"); 
+3
source

You are missing the mysql_query function, it should look something 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); 
0
source

As said, you are missing the mysql_query function.
Although the whole approach is wrong. You should not select the whole ata load if you only need the number of rows.
So it should 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 will not use $ aID for any database related action

0
source

Source: https://habr.com/ru/post/1311343/


All Articles