这个问题在这里已经有了答案:
mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource
(31 个回答)
7年前关闭。
你好,你能看看我的代码。
我这里有三个 undefined variable ,我真的不知道要修复什么
- Notice: Undefined index: Username in Line 70
- Notice: Undefined index: password in Line 71
- Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in Line 86
<!DOCTYPE HTML>
<?php
//Start session
session_start();
//Unset the variables stored in session
unset($_SESSION['SESS_Username']);
unset($_SESSION['SESS_Password']);
?>
<html>
<head>
<title> Ilokandroid Dynamic Web </title>
<meta name="author" content="jessiemaymasaoay and jaysonpinzon" >
<meta name="description" content="Student MiniSystem" >
<meta name="keywords" content="HTML,CSS,XML,Javascript, miniSystem Web Page" />
<style type="text/css" media="screen">
body{background: url(assets/images/back1.png) top no-repeat fixed;}
.upper
{
width:800px;height:100px; margin:auto; border: 5px; padding:2px;
}
#form
{
width:400px;height:190px; margin:auto; border: 2px solid black; padding:10px;
text-align: justify; margin-top: 150px; background:#008080 ; font-size: 1p6x;
}
</style>
</script>
</head>
</head>
<body>
<div id="slide"> </div>
<div id="form">
<form name="loginform" action="log.php" onsubmit="return validateForm()" method="post">
<table border="0" align="center" cellpadding="2" cellspacing="5">
<tr>
<td colspan="2">
<!--the code bellow is used to display the message of the input validation-->
<?php
if( isset($_SESSION['ERRMSG_ARR']) && is_array($_SESSION['ERRMSG_ARR']) && count($_SESSION['ERRMSG_ARR']) >0 )
{
echo '<ul class="err">';
foreach($_SESSION['ERRMSG_ARR'] as $msg) {
echo '<li>',$msg,'</li>';
}
echo '</ul>';
unset($_SESSION['ERRMSG_ARR']);
}
?>
</td>
</tr>
<tr>
<?php
// Grab User submitted information
$email = $_POST["Username"];
$pass = $_POST["password"];
// Connect to the database
$con = mysql_connect("localhost","root","");
// Make sure we connected succesfully
if(! $con)
{
die('Connection Failed'.mysql_error());
}
// Select the database to use
mysql_select_db("ilokandroid",$con);
$result = mysql_query("SELECT Username, password FROM user WHERE Username = $email");
$row = mysql_fetch_array($result);
if($row["Username"]==$email && $row["password"]==$pass)
echo"You are a validated user.";
else
echo"Sorry, your credentials are not valid, Please try again.";
?>
<td width="116"><b> Username : </b> </div></td>
<td width="177">
<input type="text" name="Username" maxlength="40"/">
</td>
</tr>
<tr>
<td> <b> Password : </b> </div></td>
<td><input name="Password" type="password" /></td>
</tr>
<tr>
<td> </td> </td>
<td width="177"><input name="remember" type="submit" value="Log In" /></td>
</tr>
<tr>
<td width="177"> Doesn't have an account? </td> <td> <a href="index.php"> Sign Up </a> </td>
</tr>
</table>
</form>
</div>
</body>
</html>
最佳答案
您的查询失败,因为您没有在字符串值周围加上引号
$result = mysql_query("SELECT Username, password FROM user WHERE Username = $email");
应该
$result = mysql_query("SELECT Username, password FROM user WHERE Username = '$email'");
Please, don't use
mysql_*
functions in new code .它们不再维护 and are officially deprecated .见red box ?了解 prepared statements而是使用 PDO或 MySQLi - this article将帮助您决定哪个。如果选择PDO,here is a good tutorial .您也对 SQL injections 敞开心扉
关于php - undefined index ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22867137/