Log in

View Full Version : Fatal error: Call to a member function querry() on a non-object



sahilmohile15
03-24-2016, 06:23 PM
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

DyDr
03-26-2016, 02:37 PM
Your code is making a database connection using the php mysql extension. The rest of your code is using statements from the php mysqli extension. You cannot mix statements from the different database extensions.

Since the php mysql extension has been removed from php (December of 2015), you should not be writing any new code using that extension. The PDO extension is a better choice over using the mysqli extension.

Next, regardless of which extension you use, your first fetch statement is mixing both OOP and procedural syntax, which won't work and you also have a typo in assoc.

You should also not be using the global keyword. It has no affect unless used inside of a function definition, and even there it breaks encapsulation of the function's code/scope. If you need to pass a value into a function, it should be passed as a call-time parameter.

Lastly, you should not be running queries inside of loops. This results in a measurable loss of performance. You should form one JOINed query that gets the rows you want in the order that you want them, then just loop over the result and output them the way you want. This will simplify your code as well.