Home php Error “mysql_fetch_array () expects parameter 1 to be resource”

Error “mysql_fetch_array () expects parameter 1 to be resource” [duplicate]

Author

Date

Category

I am doing a recursive function to display a menu with an undefined number of ancestors and descendants.
Here is the code:

& lt ;?
mysql_connect ("localhost", "root", "");
mysql_select_db ("BD");
// $ zapros = mysql_query ("select * from test where id = 0");
// echo ($ zapros);
function menu ($ parent, $ h)
{
  $ h ++;
  $ zapros = mysql_query ("select * from test where id = {$ parent}");
  while ($ row = mysql_fetch_array ($ rezult)) {
    echo "{$ row ['name']} & lt; p & gt;";
    $ zapros1 = mysql_query ("select * from test where key = {$ row ['id']}");
    while ($ row1 = mysql_fetch_array ($ zapros1)) {
      echo "& lt; p & gt; {$ row1 ['name']} & lt; p & gt;";
      $ p = $ row1 ['id'];
      menu ($ p, $ h);
    }
  }
}
menu (0, 0);
? & gt;

Database table:

CREATE TABLE `test` (
 `ID` int (11) NOT NULL AUTO_INCREMENT,
 `name` varchar (20) DEFAULT NULL,
 `key` int (11) DEFAULT NULL,
 PRIMARY KEY (`ID`),
 UNIQUE KEY `ID` (` ID`)
) ENGINE = MyISAM AUTO_INCREMENT = 11 DEFAULT CHARSET = cp1251;

As a result, he writes to me:

Warning: mysql_fetch_array () expects parameter 1 to be resource, null given in C: \ xampplite \ htdocs \ test.php on line 16


Answer 1, authority 100%

$ zapros = mysql_query ("select * from test where id = {$ parent}");

You have:

while ($ row = mysql_fetch_array ($ rezult)) {...

And it is necessary: ​​

while ($ row = mysql_fetch_array ($ zapros)) {...

Answer 2

Try connecting like this:

$ Link = mysql_connect ($ DBHost, $ DBUser, $ DBPassword) or die (mysql_error ());
mysql_select_db ($ DBName, $ Link) or die (mysql_error ());

Answer 3


Answer 4

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions