php mysql比较数据

时间:2012-04-25 01:11:46

标签: php mysql

好的,我有这段代码:

<?//admin
$host = 'localhost'; // Host name Normally 'LocalHost'
$user = 'root'; // MySQL login username
$pass = ''; // MySQL login password
$database = 'juliver'; // Database name
$table = 'admin'; // Members name

mysql_connect($host, $user, $pass);
mysql_select_db($database);
if (isset($_POST['acc']) && ($_POST['pass']))
{
$username = mysql_real_escape_string($_POST['acc']);
$password = hash('sha512', $_POST['pass']);

$result = mysql_query("SELECT * FROM admin WHERE username = '$username' AND password =     '$password'");

if(mysql_num_rows($result))
{
  // Login
echo "has been login";
}
else
{
  // Invalid username/password
  echo 'Error: Invalid username or password';
}

exit;

}
else
{
echo "not been set";
}
?>

我得到了这个错误:“警告:mysql_num_rows()期望参数1是资源,在第17行的C:\ xampp \ htdocs \ mu2 \ admin.php中给出布尔值 已经登录了“,有人在这里,可以帮我弄清楚那可能到底是什么错误吗?...提前感谢

无论如何,我打开任何建议。

3 个答案:

答案 0 :(得分:0)

简单地说你对mysql_query的调用失败了,你可以在它之后echo mysql_error()查看错误是什么。

答案 1 :(得分:0)

你的MySQL查询有些问题,你可能正在访问那些不存在或拼写错误的东西。回应mysql_error()会告诉你你的问题。我还建议消除查询中的额外空格(看起来有点奇怪)。 :)

答案 2 :(得分:0)

这可以帮助您找到错误

error_reporting(E_ALL);
ini_set('display_errors','On');
$host = 'localhost'; // Host name Normally 'LocalHost'
$user = 'root'; // MySQL login username
$pass = ''; // MySQL login password
$database = 'juliver'; // Database name
$table = 'admin'; // Members name

$link = mysql_connect ( $host, $user, $pass );

if (! $link) {
    die ( 'Could not connect: ' . mysql_error ());
}

$selected = mysql_select_db ( $database );

if (! $selected) {
    die ( 'Can\'t use foo : ' . mysql_error ());
}

if (isset ( $_POST ['acc'] ) && ($_POST ['pass'])) {
    $username = mysql_real_escape_string ( $_POST ['acc'] );
    $password = hash ( 'sha512', $_POST ['pass'] );

    $result = mysql_query ( "SELECT * FROM admin WHERE username = '$username' AND password =  '$password'" );

    if (!$result) {
        die('Invalid query: ' . mysql_error());
    }

    $num_rows = mysql_num_rows($result);

    if ($num_rows > 0) {
        // Login
        echo "has been login";
    } else {
        // Invalid username/password
        echo 'Error: Invalid username or password';
    }

    exit ();

} else {
    echo "not been set";
}