0

Hello, I am working on a project for online store but i am getting the error Fatal error: Call to a member function querry() on a non-object
i have tried everything i can think of, please help me, i am in a bit hurry.
Here's my code

init.php :

<?php
$host = 'localhost';
$user = 'root';
$password = '';
$database = 'store';



$db = mysql_connect($host, $user, $password, $database);

$check = mysqli_connect_errno();


if ($check) {
    echo "Database Connection failed because of following error". mysqli_connect_error();
    die();
}


?>

And here's my navigation.php :

<?php
    $sql= "SELECT * FROM categories WHERE parent = 0";
    global $db;

    $pquery = $db -> query($sql);

?>


<!--Nav Bar -->
        <nav class="navbar navbar-default navbar-fixed-top">
            <div class="container">
                <a href="index.php" class="navbar-brand">Online Store</a>
                <ul class="nav navbar-nav">

                    <?php while ($parent = $pquery -> mysqli_fetch_assc($pquery)): //While Statement?>
                        <?php $parent_id = $parent['id']; 
                            $sql2 = "SELECT * FROM categories WHERE parent = parent_id";
                            $cquerry = $db->query ($sql2);
                        ?>

                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle= "dropdown"><?php echo $parent['categories']; ?><span class="caret"></span></a>
                        <ul class="dropdown-menu" role="menu">

                            <?php while ($child = mysqli_fetch_assoc($cquery)): //while statement?>
                            <li><a href="#"><?php echo $child['category']; ?></a></li>

                            <?php endwhile; // end of the while?> 
                        </ul>
                    </li>
                    <?php endwhile; //end of the while ?>

                </ul>
            </div>
        </nav>

please help me as soon as you can.
Thank you in advance

Edited by sahilmohile15: There was a little mistake in code

2
Contributors
4
Replies
18
Views
1 Year
Discussion Span
Last Post by sahilmohile15
1

If you do not include init.php in your navigation.php then the $db variable will not be initialized. Hence the error.

0

I included it but still there is error here's my new navigation.php

<?php
    include 'core/init.php';
    $sql= "SELECT * FROM categories WHERE parent = 0";
    global $db;
    $pquery = $db->query($sql);

?>
1

Your init file uses mysql_connect instead of mysqli_connect. The mysql version does not return an object instance.

Votes + Comments
thanks that really helped me i had to switch it to mysql_connect from mysqli_connect because it wasn't connecting anyway thank you
0

I am having another error in navigation.php
** mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given **
this is my error and this is updated navigation.php

<?php
    include 'core/init.php';
    $sql= "SELECT * FROM categories WHERE parent = 0";
    global $db;
    $pquery = $db->query($sql);

?>

<!--Nav Bar -->
        <nav class="navbar navbar-default navbar-fixed-top">
            <div class="container">
                <a href="index.php" class="navbar-brand">Sahil's Store</a>
                <ul class="nav navbar-nav">

                    <?php while ($parent = mysqli_fetch_assoc($pquery)): //While Statement?>
                        <?php $parent_id = $parent['id']; 
                            $sql2 = "SELECT * FROM categories WHERE parent = parent_id";
                            $cquery = $db->query ($sql2);
                        ?>

                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle= "dropdown"><?php echo $parent['categories']; ?><span class="caret"></span></a>
                        <ul class="dropdown-menu" role="menu">

                            <?php while ($child = mysqli_fetch_assoc($cquery)): //while statement?>
                            <li><a href="#"><?php echo $child['category']; ?></a></li>

                            <?php endwhile; // end of the while?> 
                        </ul>
                    </li>
                    <?php endwhile; //end of the while ?>

                </ul>
            </div>
        </nav>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.