Deepashika Deepashika - 4 months ago 6
SQL Question

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result,

I need to select data from the database to display user profile, when I select and display it gives the error:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\wamp\www\final1\can_dash.php on line 89

Php code is given bellow



<?php session_start(); include_once 'db.php'; $sql="SELECT * FROM candi_profile WHERE ur_email='{$_SESSION['usr_email']}'" ; $result=mysqli_query($con,$sql); ?>

<body>
<!-- Navigation -->
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<img src="assets/images/logo1.png" alt="logo">
</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="navbar1">
<ul class="nav navbar-nav navbar-right">
<?php if (isset($_SESSION[ 'usr_id'])) { ?>
<li>
<p class="navbar-text">Signed in as
<?php echo $_SESSION[ 'usr_name']; ?>
</p>
</li>
<li><a href="logout.php">Log Out</a>
</li>
<?php } else { ?>
<li><a href="login.php">Login</a>
</li>
<li><a href="register.php">Sign Up</a>
</li>
<?php } ?>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container -->
</nav>
<br>
<br>
<!-- Page Content -->



<div class="container">
<!-- Page Header -->
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">Employer Dashboard

</h1>
</div>
</div>
<!-- /.row -->

<!-- Projects Row -->

<div class="row">
<div class="col-md-4">
<?php while($rows=mysqli_fetch_array($result)){ ?>

<p class="lead">
<?php echo $rows[ 'can_name'] ?>
</p>
<div class="profile-sidebar">
<!-- SIDEBAR USERPIC -->

<div class="profile-userpic">
<img src="assets/images/new.png" class="img-responsive" alt="">
</div>
<!-- END SIDEBAR USERPIC -->

<!-- SIDEBAR USER TITLE -->
<div class="profile-usertitle">
<div class="profile-usertitle-name">
Marcus Doe
</div>
<div class="profile-usertitle-job">
<?php echo $rows[ 'can_city'] ?> <i class="glyphicon glyphicon-map-marker">
</i>
</div>

<div class="profile-usertitle-job">
<i class="glyphicon glyphicon-envelope"></i>
<?php echo $rows[ 'can_email'] ?>
</div>

<div class="profile-usertitle-job">
<?php echo $rows[ 'can_country'] ?>
</div>
</div>
<!-- END SIDEBAR USER TITLE -->

<!-- SIDEBAR BUTTONS -->
<div class="profile-userbuttons">
<hr>
</div>
<!-- END SIDEBAR BUTTONS -->
<!-- SIDEBAR MENU -->

<?php } ?>
</div>







Answer

The problem is that between this line:

$result=mysqli_query($con,$sql);

And this line:

while($rows=mysqli_fetch_array($result))

You never check whether the query was run successfully. When the query fails, if you try to use its $result you'll get this error. Change the 2nd line to:

while($result && $rows=mysqli_fetch_array($result))

And the error will go away. However if the query errors, the while loop will be skipped (your sidebar won't be built). If you want to see the error, check for it after executing the query:

$result=mysqli_query($con,$sql);
if(!$result) die(mysqli_error($conn));

This will abort the script and show you the error. Be sure to remove it before production because end users should not see details about your server error.

Comments