计算行时出错

时间:2011-08-14 07:23:02

标签: php mysql

我正在登录,我在计算MySQL查询的行数时遇到错误。错误如下。

 Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /home/nzcraftn/public_html/bs/login.php on line 13

Warning: Cannot modify header information - headers already sent by (output started at /home/nzcraftn/public_html/bs/login.php:13) in /home/nzcraftn/public_html/bs/login.php on line 21

我正在使用此代码进行登录。

<?php
session_start();
include('config.php');

$user = protect($_POST['username']);
$pass = protect($_POST['password']);

$salt = "a123b123";
$thepass = md5($salt.md5($pass));

$res = mysql_query("SELECT `username`,`password` WHERE (`username` = '$user' AND `password` = '$thepass')");

$count = mysql_num_rows($res);

if($count == 1) {
$_SESSION['user'] = $user;
print($_SESSION['user']);
//header('Location: /panel/index.php');
} else {
$_SESSION['errormsg'] = "<div style='color: #FF0000'>Invalid Login!</div>";
header('Location: index.php');
}
?>

2 个答案:

答案 0 :(得分:2)

您的陈述中有错误,因此mysql_query正在返回false。请在代码中添加错误检查(以及记录调试)。

(您的SELECT缺少FROM条款。)

答案 1 :(得分:1)

通过自己在MySQL提示符中运行SQL查询,确保SQL查询正常工作。在SQL查询周围添加一些故障排除代码,如下所示:

$query = 'SELECT ...';
$res = mysql_query($query) or die(mysql_error()."\n".$query);