Click here to Skip to main content
15,902,189 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having a small problem. please help me to resolve it.

my code is displaying this error:
( ! ) Fatal error: Call to a member function fetch_array() on a non-object on line 44

this is my code:
PHP
<?php
$dbcon=mysql_connect("localhost","root","");
$link = mysql_connect('localhost','test',''); 
if (!$link) { 
	die('Could not connect to MySQL: ' . mysql_error()); 
} 
echo 'Connection OK'; mysql_close($link); 
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>front page</title>
<script type="text/javascript"  src="jquery-3.4.1.min.map"></script>
<link rel="stylesheet" type="text/css" href="style.css" media="all" />
</head>
<body>
<div id="head">
<div class="wrap"><br />
<h1 align="center">Create your own Menu Bar</h1>
<label><a href="add_menu.php">Click to Add Menu</a></label>
</div>
</div>

<div class="wrap">
<ul id="nav">
<li><a href="#">Homepage</a></li>
<?php
$res=$dbcon->query("SELECT * FROM main_menu");
while($row=$res->fetch_array())
{
 ?>
 <li><a href="<?php echo $row['m_menu_link']; ?>"><?php echo $row['m_menu_name']; ?></a>
 <?php
 $res_pro=$dbcon->query("SELECT * FROM sub_menu WHERE m_menu_id=".$row['m_menu_id']);
 ?>
        <ul>    
  <?php  
  while($pro_row=$res_pro->fetch_array())
  {
   ?><li><a href="<?php echo $pro_row['s_menu_link']; ?>"><?php echo $pro_row['s_menu_name']; ?></a></li><?php
  }
  ?>
 </ul>
 </li> 
 
<?php
}
?>
</ul> 
</div>
<script type="text/javascript">
$(document).ready(function() 
{
 $('#nav li').hover(function() 
 {
  $('ul', this).slideDown('fast');
 }, function() 
 {
  $('ul', this).slideUp('fast');
 });
});
</script>
</body>
</html>


What I have tried:

it is giving me error at this:
while($row = $res->fetch_array())
Posted
Updated 9-Jan-20 10:59am
v4

It appears you are mixing MySql APIs and/or styles; which are incompatible with one another.

This is using the old mysql API: $dbcon=mysql_connect("localhost","root","");
While this appears to be either mysqli or PDO while($row = $res->fetch_array())

I am a master of neither MySql nor PHP... so you may need to do some further research. I would recommend you review these links:
PHP: Choosing an API - Manual[^]
PHP: mysqli_result::fetch_array - Manual[^]
 
Share this answer
 
I know little of PHP, but the top answer here might be of use. It's possible $dbcon has not been declared correctly. PHP and MYSQLI Error, Call to a member function query() on a non-object - Stack Overflow[^]
 
Share this answer
 
v2
Comments
[no name] 9-Jan-20 15:29pm    
i have solved that isssue. now it is giving me a new error:

Fatal error: Call to a member function fetch_array() on a non-object on line 44
here is the line 44
while($row = $res->fetch_array())
Kris Lantz 9-Jan-20 15:49pm    
Where is $res fetching an array from? Does fetch take a param?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900