Nistor Alexandru
Nistor Alexandru

Reputation: 5393

MySQL query returns a false value

I am trying to make a menu using the data from a database table. But I have a little problem. My query results return a false boolean value.

This is my code:

<?php
class DataBase
{
    protected $dbUser;
    protected $dbPassword;
    protected $dbHost;
    protected $dbName;
    protected $db;

    function __construct()
    {
        $this->dbUser     = 'root';
        $this->dbPassword = '';
        $this->dbHost     = 'localhost';
        $this->dbName     = 'ecommercemakup';
        $this->db = mysqli_connect($this->dbHost, $this->dbUser, $this->dbPassword, $this->dbName) or die('Fatal error!');
    }

    public function getInstance()
    {
        return $this->db;
    }
}
?>

<?php
$db = new DataBase();
$r  = mysqli_query($db->getInstance(), "CALL categories()");
if (mysqli_num_rows($r) > 0) {
    while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
        echo '<li> <a href=' . $row['name'] . '.php>';
        print_r($row['name']);
        echo '</a>
            <ul>';
        $id = intval($row['idCategory']);
        $r1 = mysqli_query($db->getInstance(), "CALL subcategories($id)");
        if (mysqli_num_rows($r1) > 0) {
            while ($row1 = mysqli_fetch_array($r1, MYSQLI_ASSOC)) {
                echo '<li><a href=' . $row1['name'], '.php>';
                print_r($row1['name']);
                echo '</a></li>';
            }
        }
        echo '</ul></li>';
    }
}
?>

The first query runs correctly, but the second query always return false.

The two procedures are this:

DELIMITER @@
DROP PROCEDURE categories @@
CREATE PROCEDURE ecommercemakup.categories()
BEGIN
    SELECT * FROM categories;
END @@ 
DELIMITER ; 

DELIMITER @@
DROP PROCEDURE subcategories @@
CREATE PROCEDURE ecommercemakup.subcategories
(IN id_Category INT)
BEGIN
    SELECT idSubcategory, idCategory, name FROM subcategories WHERE idCategory = id_Category;
END @@ 
DELIMITER ; 

The categories table contains 2 items: idCategory and name, and the subcategories table contain 3 items: idSubcategory, idCategory and name.

Can someone tell me what I am doing wrong here?

Upvotes: 3

Views: 703

Answers (1)

Meherzad
Meherzad

Reputation: 8563

Replace the variable name to @id_Category, you are missing @ before the name.

In MySQL, @variable indicates a user-defined variable. You can define your own.

SET @a = 'test'; SELECT @a;

A variable, without @, is a system variable, which you cannot define yourself. This is causing problem for you, as you are declaring variable without @

Hope it helps

REFER

User defined variable

System variable

Upvotes: 1

Related Questions