【问题标题】:mysql can not compare data and parse it with phpmysql无法比较数据并用php解析
【发布时间】:2013-11-29 06:39:31
【问题描述】:

我要回去制作一些 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";
            }
        }       
    }
?>

希望你能理解,希望你能帮助我。我不开心。

谢谢

【问题讨论】:

  • doesn't work 是什么意思?黑屏?错误信息?让大家进去?不让一个人进来?让你在某些时候?
  • mysql_* 函数不再支持,它们是 officially deprecated不再维护,将在未来。您应该使用PDOMySQLi 更新您的代码,以确保您的项目在未来的功能。
  • $x的值是多少?
  • 我登录正确但总是显示错误信息 'cause $num == 0
  • 问题不清楚,X的值未知,没有给出错误信息。您希望我们如何帮助您?

标签: php mysql md5


【解决方案1】:

希望它对您有所帮助,$num = mysql_num_rows($result); 而不是 $num = $result1-&gt;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 扩展。

【讨论】:

  • 那么$result1 呢?
  • ...你有它。 ;-)
  • 是的,弗雷德,是因为你。再次感谢您。
  • 我们在一起,非常欢迎您。称之为“共同努力”;-)
【解决方案2】:

工作代码:

 <?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...btw mysqli in procedural mode 和mysql extension很相似。

【讨论】:

    猜你喜欢
    • 2018-06-30
    • 2013-04-06
    • 1970-01-01
    • 1970-01-01
    • 2020-10-05
    • 1970-01-01
    • 1970-01-01
    • 2016-02-14
    • 1970-01-01
    相关资源
    最近更新 更多