Ted Ted - 2 months ago 15
MySQL Question

Warning: mysql_fetch_row() expects parameter 1

I'm a new learner in PHP. I don't know what's my error with my code. Please help me fix this. :( Warning: mysql_fetch_row() expects parameter 1 to be resource, object given in C:\xampp\htdocs\project\employees.php on line 35.

connection.php

<?php

//print_r($_POST);

// DB Credentials
$servername = "localhost";
$dbusername = "root";
$dbpassword = "";
$dbname = "project101";

// Create connection
$con = mysqli_connect($servername, $dbusername, $dbpassword, $dbname);

// Check connection
if ( $con->connect_error ) {
die("Connection failed: " . mysqli_connect_error());
}



?>


Employee.php


include ('connection.php');
if(empty($_SESSION)) // if the session not yet started
session_start();

if(!isset($_SESSION['username'])) { //if not yet logged in
header("Location: index.php");// send to login page
exit;
}
?>

<html>
<head>

<title>Employee</title>

</head>
<body>
<center>

<div id="body">
<div id="content">
<table align="center">
<tr>
<th colspan="5"><a href="add.php">add data here.</a></th>
</tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th colspan="2">Action</th>
</tr>
<?php
$result2 = $con->query("Select * from `employee`");
while($row=mysql_fetch_row($result2))
{
?>
<tr>
<td><?php echo $row[1]; ?></td>
<td><?php echo $row[2]; ?></td>
<td><?php echo $row[3]; ?></td>
<td align="center"><a href="javascript:edt_id('<?php echo $row[0]; ?>')"><img src="b_edit.png" align="EDIT" /></a></td>
<td align="center"><a href="javascript:delete_id('<?php echo $row[0]; ?>')"><img src="b_drop.png" align="DELETE" /></a></td>
</tr>
<?php
}
?>
</table>
</div>
</div>

</center>
</body>
</html>

Answer

Use the following code

include ('connection.php');
if(empty($_SESSION)) // if the session not yet started 
   session_start();

if(!isset($_SESSION['username'])) { //if not yet logged in
   header("Location: index.php");// send to login page
   exit;
} 
?>

<html>
<head>

<title>Employee</title>

</head>
<body>
<center>

<div id="body">
 <div id="content">
    <table align="center">
    <tr>
    <th colspan="5"><a href="add.php">add data here.</a></th>
    </tr>
    <th>First Name</th>
    <th>Last Name</th>
    <th>Email</th>
    <th colspan="2">Action</th>
    </tr>
    <?php
 $result2 = $con->query("Select * from `employee`");
 while($row=mysqli_fetch_row($result2))
 {
  ?>
        <tr>
        <td><?php echo $row[1]; ?></td>
        <td><?php echo $row[2]; ?></td>
        <td><?php echo $row[3]; ?></td>
  <td align="center"><a href="javascript:edt_id('<?php echo $row[0]; ?>')"><img src="b_edit.png" align="EDIT" /></a></td>
        <td align="center"><a href="javascript:delete_id('<?php echo $row[0]; ?>')"><img src="b_drop.png" align="DELETE" /></a></td>
        </tr>
        <?php
 }
 ?>
    </table>
    </div>
</div>

</center>
</body>
</html>
Comments