-2

I am trying to convert the following script from mysql to mysqli but am having problems.

This is the orginal script using mysql :

    //database connection
    $dbname = 'nursery';
    $link = mysql_connect("localhost","root","") or die("Couldn't make connection.");
    $db = mysql_select_db($dbname, $link) or die("Couldn't select database");
    ?>
    <div style="width:728px;margin:auto;">
        <div id='cssmenu'>
            <ul>
    <?php
    function query($parentid) { //function to run a query
        $query = mysql_query ( "SELECT * FROM menu WHERE parentid=$parentid" );
        return $query;
    }
    function has_child($query) { //This function checks if the menus has childs or not
        $rows = mysql_num_rows ( $query );
        if ($rows > 0) {
            return true;
        } else {
            return false;
        }
    }
    function fetch_menu($query) {
        while ( $result = mysql_fetch_array ( $query ) ) {
            $menu_id = $result ['id'];
            $title = $result ['title'];
            $url = $result ['url'];
            echo "<li  class='has-sub '><a href='{$url}'><span>{$title}</span></a>";
            if (has_child ( query ( $menu_id ) )) {
                echo "<ul>";
                fetch_menu ( query ( $menu_id ) );
                echo "</ul>";
            }
            echo "</li>";
        }
    }
    fetch_menu (query(0)); //call this function with 0 parent id
    ?>
            </ul>
        </div>
        </div>

So I am trying to convert it into mysqli which the rest of my site uses and have got this far :

    <?php
    require("../login/common.php");
    //database connection;
    include '../connect.php';
    ?>
    <div style="width:728px;margin:auto;">
        <div id='cssmenu'>
            <ul>
    <?php
    function query($parentid) { //function to run a query
        $query = mysqli_query ($db, "SELECT * FROM menu WHERE parentid=$parentid" );
        return $query;
    }
    function has_child($query) { //This function checks if the menus has childs or not
        $rows = mysqli_num_rows ( $query );
        if ($rows > 0) {
            return true;
        } else {
            return false;
        }
    }
    function fetch_menu($query) {
        while ( $result = mysqli_fetch_array ( $query ) ) {
            $menu_id = $result ['id'];
            $title = $result ['title'];
            $url = $result ['url'];
            echo "<li  class='has-sub '><a href='{$url}'><span>{$title}</span></a>";
            if (has_child ( query ( $menu_id ) )) {
                echo "<ul>";
                fetch_menu ( query ( $menu_id ) );
                echo "</ul>";
            }
            echo "</li>";
        }
    }
    fetch_menu (query(0)); //call this function with 0 parent id
    ?>
            </ul>
        </div>
        </div>

The problem is I am getting all kinds of errors and am not totally sure why, I know the db error is probably down to the $db not being passed into the function, but even if I write function fetch_menu($query,$db) { I still get the same error ?.

Notice: Undefined variable: db in         C:\easyphpserver\data\localweb\projects\nursery\menu\menu3.php on line 147

  Warning: mysqli_query() expects parameter 1 to be mysqli, null given in    C:\easyphpserver\data\localweb\projects\nursery\menu\menu3.php on line 147

   Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in       C:\easyphpserver\data\localweb\projects\nursery\menu\menu3.php on line 159

The contents of connect.php is :

 <?php
$db = new mysqli('localhost', 'root', '', 'nursery');
$db->set_charset('utf8mb4');  
  if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}  
 ?>
Iain Simpson
  • 441
  • 4
  • 13
  • 29

2 Answers2

0

pass $db to query()

function query($parentid, $db) {
    $query = mysqli_query ($db, "SELECT * FROM menu WHERE parentid=$parentid" );
    return $query;
}
meda
  • 45,103
  • 14
  • 92
  • 122
0

Ok, so this solution seems to work, by putting the db connection inside the function, the thing is if I include the $db in the function by typing function query($parentid,$db) { this doesn't work and causes errors, but if I put the db connection inside the function it works.....very odd.

    <?php
    //error_reporting(0);
    require("../login/common.php");
    //database connection;
    include '../connect.php';
    ?>
    <div style="width:728px;margin:auto;">
        <div id='cssmenu'>
            <ul>
    <?php
    function query($parentid) {
        $db = new mysqli('localhost', 'root', '', 'nursery');
    $db->set_charset('utf8mb4');  
    if($db->connect_errno > 0){
        die('Unable to connect to database [' . $db->connect_error . ']');
    }
         //function to run a query
        $query = mysqli_query ($db, "SELECT * FROM menu WHERE parentid=$parentid" );
        return $query;
    }
    function has_child($query) { //This function checks if the menus has childs or not
        $rows = mysqli_num_rows ( $query );
        if ($rows > 0) {
            return true;
        } else {
            return false;
        }
    }
    function fetch_menu($query) {
        while ( $result = mysqli_fetch_array ( $query ) ) {
            $menu_id = $result ['id'];
            $title = $result ['title'];
            $url = $result ['url'];
            echo "<li  class='has-sub '><a href='{$url}'><span>{$title}</span></a>";
            if (has_child ( query ( $menu_id ) )) {
                echo "<ul>";
                fetch_menu ( query ( $menu_id ) );
                echo "</ul>";
            }
            echo "</li>";
        }
    }
    fetch_menu (query(0)); //call this function with 0 parent id
    ?>
            </ul>
        </div>
        </div>
Iain Simpson
  • 441
  • 4
  • 13
  • 29