mysql无法比较数据并用php解析它

时间:2013-11-15 18:47:20

标签: php mysql md5

我回来制作一些PHP代码来制作登录/注册表单。 帐户数据存储在mysql数据库中,我使用该代码验证md5并通过浏览器解析它。但是有一些错误,因为它不起作用。

<?php
    if (!$x) {
        echo "<script type='text/javascript'>document.location.href='index.php';</script>";
    } else {
        $con = mysql_connect('localhost', 'userdb', 'pwd') or die(mysql_error());
        $db = mysql_select_db('dbname', $con) or die(mysql_error());
        $result = mysql_query("SELECT * FROM `users` WHERE `md5pwd` = '". $x ."'");
        $num = $result1->num_rows;
        if($num == 0){
            //if not display an error message
            echo "<script> alert('Usuario inexistente') </script>";
            echo "<script type='text/javascript'>document.location.href='index.php';</script>";
        }else{
            while($row=mysql_fetch_object($result1)){
                $userName=$row->username; 
                echo "<script> alert('BIENVENIDO " . $userName . "') </script>";
                echo "[ " . $userName . " ]\n\n";
            }
        }       
    }
?>

我希望你能理解,我希望你能帮助我。我很不高兴。

感谢

2 个答案:

答案 0 :(得分:3)

希望它对您有帮助,$num = mysql_num_rows($result);代替$num = $result1->num_rows;

mysql_fetch_object($result)代替mysql_fetch_object($result1)

<?php
    if (!$x) {
        echo "<script type='text/javascript'>document.location.href='index.php';</script>";
    } else {
        $con = mysql_connect('localhost', 'userdb', 'pwd') or die(mysql_error());
        $db = mysql_select_db('dbname', $con) or die(mysql_error());
        $result = mysql_query("SELECT * FROM `users` WHERE `md5pwd` = '". $x ."'");
        $num = mysql_num_rows($result);
        if($num == 0){
            //if not display an error message
            echo "<script> alert('Usuario inexistente');document.location.href='index.php';</script>";
        }else{
            while($row=mysql_fetch_object($result)){
                $userName=$row->username; 
                echo "<script> alert('BIENVENIDO " . $userName . "') </script>";
                echo "[ " . $userName . " ]\n\n";
            }
        }       
    }
?>

注意: mysql_ *函数自PHP 5.5.0起已弃用,将来会被删除。相反,应该使用MySQLi或PDO_MySQL扩展。

答案 1 :(得分:1)

工作代码:

 <?php

        if (!isset($x)) {
            echo "<script type='text/javascript'>document.location.href='index.php';</script>";
        } else {
            $con = mysql_connect('localhost', 'userdb', 'pwd') or die(mysql_error());
            $db = mysql_select_db('dbname', $con) or die(mysql_error());
            $result = mysql_query("SELECT * FROM `users` WHERE `md5pwd` = '". $x ."'");
            $num = mysql_num_rows($result);
            if($num == 0){
                //if not display an error message
                echo "<script> alert('Usuario inexistente') </script>";
                echo "<script type='text/javascript'>document.location.href='index.php';</script>";
            }else{
                while($row=mysql_fetch_object($result)){
                    $userName=$row->username; 
                    echo "<script> alert('BIENVENIDO " . $userName . "') </script>";
                    echo "[ " . $userName . " ]\n\n";
                }
            }       
        }
    ?>

PS:将来会删除mysql_extension,使用mysqli或PDO ...在程序模式下使用mysqli,它与mysql扩展非常相似。