大家好我是php和html的初学者,你可以帮我解决这个问题吗?
它给了我这个错误Warning: oci_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\login.php on line 15
这是我的代码:
<?php
session_start();
// connect to database
if (isset($_POST['login_btn'])) {
$username =$_POST['username'];
$password =$_POST['password'];
$conn = oci_connect('socialdb', '12345', 'localhost/XE');
if (!$conn) {
$e = oci_error();
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
$password = md5($password); // remember we hashed password before storing last time
$sql = oci_parse($conn,"SELECT * FROM ACCOUNT WHERE username='$username' AND password='$password'");
$result =oci_execute($sql);
if (oci_num_rows($result) == 1) {
$_SESSION['message'] = "You are now logged in";
$_SESSION['username'] = $username;
header("location: home.php"); //redirect to home page
} else{
$_SESSION['message'] = "Username/password combination incorrect";
}
}
?>
更新:可能问题出在HTML表单中:
<!DOCTYPE html>
<html>
<head>
<title>Register, login and logout user php oracle</title>
<link rel="stylesheet" type="text/css" href="stylea.css">
</head>
<body>
<div class="header">
<h1>Register, login and logout user php oracle</h1>
</div>
<?php
if (isset($_SESSION['message'])) {
echo "<div id='error_msg'>".$_SESSION['message']."</div>";
unset($_SESSION['message']);
}
?>
<form method="post" action="login.php">
<table>
<tr>
<td>Username:</td>
<td><input type="text" name="username" class="textInput"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password" class="textInput"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="login_btn" value="Login"></td>
</tr>
</table>
</form>
</body>
</html>
&#13;
谢谢你,抱歉我的英语不好。
答案 0 :(得分:1)
首先尝试检查查询结果的有效性,然后注意,因为oci_execute返回一个布尔值,请参考this link作为示例
$result = oci_execute($sql);
if ($result) {
if (oci_num_rows($sql) == 1) {
$_SESSION['message'] = "You are now logged in";
$_SESSION['username'] = $username;
header("location: home.php"); //redirect to home page
} else{
$_SESSION['message'] = "Username/password combination incorrect";
}
} else {
$_SESSION['message'] = "Query error";
}