Solution to PHP Notice: Undefined variable: db

1

This is the code:

function getCategoryTree($level = 0, $prefix = '')
{
    //$sqll = "SELECT * FROM expense_categories WHERE master_category = ".$level." GROUP BY category_id ORDER BY category ASC";
  echo $db."<br/><br/>\n";
    $result=mysqli_query($db,"SELECT * FROM expense_categories WHERE master_category = '$level' GROUP BY category_id ORDER BY category ASC"); //Linea 51
    if(mysqli_num_rows($result)>0)
    {
        while($row = mysqli_fetch_array($result)){
            $category .= $prefix . $row['category']."~~".$row['category_id'] . "<br>";
            //$categories[$row->category_id] = $prefix .$row->category_name;
            // Append subcategories
            $category .=getCategoryTree($row['category_id'], $prefix . '>');
        }

    }
    return $category;
}

The error:

  

PHP Notice: Undefined variable: db in   /var/www/misitio.org/cc/config.inc on line 51

I do not understand why this happens, if the variable is declared in the connection file: dbase.inc

<?php
#################CONFIGURATION FILE############################## 
define("TEMPLATES", "templates/");
## EXPENSE Categories

$expense_categories = array('Gas', 'Travel', 'Food');
##Usually this is just localhost
$host = 'localhost';
## enter mysqli username
$user = 'root';
## enter mysqli password
$pass = 'toor';
## enter databse name
$database = 'rootdb';
## start Database
$db = mysqli_connect($host, $user, $pass)
        or die("Could not connect: " . mysqli_error());
mysqli_select_db($db, $database);



function mysqli_fetch_array_r($result, $index_row = "", $fetch = mysqli_ASSOC){
    $n = 0;
    while ($row = mysqli_fetch_array($result,$fetch)){
        $hold_arr= &$whole_result[$row[$index_row]];
        $hold_arr[]=$row;
    }
    return $whole_result;
}
extract($_REQUEST);

?>

And this is included in the TOP of the config.inc file that is where the error originates

<?php
include('dbase.inc');
global $db;
    
asked by Martini002 30.03.2017 в 21:09
source

2 answers

1

You are not passing the variable to the function try this

function getCategoryTree($level = 0, $prefix = '',$db) {
    
answered by 31.03.2017 / 02:06
source
1

From what I see below the include you are redeclaiming the variable $db , which is overwriting the one you bring with the dbase.inc file, try removing that line of code.

    
answered by 01.04.2017 в 18:08