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

Recommended Answers

All 4 Replies

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

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);

?>

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

commented: 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>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.