我是PHP的新学习者。我不知道我的代码是什么错误。请帮我解决这个问题。 :(警告:mysql_fetch_row()期望参数1是资源,第35行的C:\ xampp \ htdocs \ project \ employees.php中给出的对象。
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>
答案 0 :(得分:1)
通过您注释的附加代码可以明显看出,您可以实现mysqli_对象,但尝试在其上使用mysql_函数。 那两个人不能一起工作。改为使用mysqli_ functions。
答案 1 :(得分:0)
使用以下代码
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>