【发布时间】:2011-05-29 22:07:59
【问题描述】:
我正在尝试在 MySql 数据库中运行查询,但每次都会收到相同的警告。有人可以帮忙吗?
警告:mysql_num_rows():在第 279 行的 /XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/public_html/login.php 中提供的参数不是有效的 MySQL 结果资源 您提供的用户名不存在!
我已经用 ++++++++++++$num = mysql_num_rows($res);++++++++++++ 突出显示了第 279 行
请告诉我如何解决这个问题。
<?php
//If the user has submitted the form
if($_POST['submit']) {
//protect the posted value then store them to variables
$username = protect($_POST['username']);
$password = protect($_POST['password']);
//Check if the username or password boxes were not filled in
if(!$username || !$password) {
//if not display an error message
echo "<center>You need to fill in a <b>Username</b> and a <b>Password</b>!</center>";
} else {
//if the were continue checking
//select all rows from the table where the username matches the one entered by the user
$res = mysql_query("SELECT *
FROM `users`
WHERE `username` = '".$username."'");
++++++++++++++$num = mysql_num_rows($res);++++++++++++++++++
//check if there was not a match
if($num == 0) {
//if not display an error message
echo "<center>The <b>Username</b> you supplied does not exist!</center>";
} else {
//if there was a match continue checking
//select all rows where the username and password match the ones submitted by the user
$res = mysql_query("SELECT * FROM `users`
WHERE `username` = '".$username."'
AND `password` = '".$password."'");
$num = mysql_num_rows($res);
//check if there was not a match
if($num == 0) {
//if not display error message
echo "<center>The <b>Password</b> you supplied does not match the one for that username!</center>";
} else {
//if there was continue checking
//split all fields fom the correct row into an associative array
$row = mysql_fetch_assoc($res);
//check to see if the user has not activated their account yet
if($row['active'] != 1){
//if not display error message
echo "<center>You have not yet <b>Activated</b> your account!</center>";
}else{
//if they have log them in
//set the login session storing there id - we use this to see if they are logged in or not
$_SESSION['uid'] = $row['id'];
//show message
echo "<center>You have successfully logged in!</center>";
//update the online field to 50 seconds into the future
$time = date('U')+50;
mysql_query("UPDATE `users` SET `online` = '".$time."' WHERE `id` = '".$_SESSION['uid']."'");
//redirect them to the usersonline page
header('Location: usersOnline.php');
}
}
}
}
}
?>
【问题讨论】:
-
保护功能在做什么?
-
您的错误表明您的查询不正确