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

<? 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']}<p>"; $zapros1 = mysql_query("select * from test where key={$row['id']}"); while ($row1 = mysql_fetch_array($zapros1)) { echo " <p>{$row1['name']}<p>"; $p = $row1['id']; menu($p, $h); } } } menu(0, 0); ?> 

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

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

Various manipulations lead nowhere. Tell me, please, what could be the problem?

Reported as a duplicate by Athari , Yura Ivanov , Maxim Kamalov , ixSci , AntonioK May 16 '15 at 6:06 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

    4 answers 4

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

    You:

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

    But you must:

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

      Try connecting the view:

       $Link = mysql_connect($DBHost, $DBUser, $DBPassword) or die(mysql_error()); mysql_select_db($DBName, $Link) or die(mysql_error()); 
      • Does not help. The connection to the base is good, the problem seems to be in the code ... - new_russian_man
      • one
        1) while ($ row = mysql_fetch_array ($ rezult)) {replace with: while ($ row = mysql_fetch_array ($ zapros)) {2) menu (0, 0); replace with: menu (11, 0); because You specified when creating the table: AUTO_INCREMENT = 11 - Avalon

      In one of your cycles, you apparently have the value from mysql_query() returned as null: for example, an error in the query.

        ID is unequal 0, try calling a function, for example menu (1, 0); unless of course you start recording with 11