【问题标题】:error message "mysql_connect(): The mysql extension is deprecated and will be removed in the future on line 6." how can i fix it? [duplicate]错误消息“mysql_connect():不推荐使用 mysql 扩展,将来将在第 6 行删除。”我该如何解决? [复制]
【发布时间】:2014-11-11 20:21:58
【问题描述】:

这是我的代码。我该如何解决? 错误信息是这样的

“不推荐使用:mysql_connect():不推荐使用mysql扩展并且 将来将被删除:使用 mysqli 或 PDO 代替 C:\wamp\www\test12345\db.php 在第 6 行"

我怎样才能创建一个没有错误的登录页面? 我正在尝试创建一个具有会话安全登录的网站?我做得对吗?

db.php

<?php
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "web";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Can't connect to database");
mysql_select_db($mysql_database, $bd) or die("Opps some thing went wrong");

登录.php

<?php
include("db.php");
session_start();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // username and password sent from Form
    $username = mysql_real_escape_string($_POST['username']);
    $password = mysql_real_escape_string($_POST['password']);
    $password = md5($password); // Encrypted Password
    $sql = "SELECT id FROM admin WHERE username='$username' and passcode='$password'";
    $result = mysql_query($sql);
    $count = mysql_num_rows($result);

    // If result matched $username and $password, table row must be 1 row
    if ($count == 1) {
        session_register("username");
        $_SESSION['login_user'] = $username;

        header("location: welcome.php");
    } else {
        $error = "Your Login Name or Password is invalid";
    }
}
?>
<form action="login.php" method="post">
    <label>UserName :</label>
    <input type="text" name="username"/><br />
    <label>Password :</label>
    <input type="password" name="password"/><br/>
    <input type="submit" value=" Login "/><br />
</form>

【问题讨论】:

  • 是时候转移到mysqli 或消息所述的 PDO。
  • 我建议你完全放弃这段代码并查看以下链接:使用CRYPT_BLOWFISH 或 PHP 5.5 的password_hash() 函数。对于 PHP password_hash() compatibility pack。另外,use prepared statementsPDO with prepared statements
  • 当我将其更改为 mysqli 时会弹出此错误消息。“警告:mysqli_select_db() 期望参数 1 为 mysqli,C:\wamp\www\test12345\db.php 中给出的字符串7号线
  • "当我将其更改为 mysqli 时会弹出此错误消息。"警告:mysqli_select_db() 期望参数 1 为 mysqli,字符串在 C:\wamp\www\test12345\第 7 行的 db.php"" - 很明显,您正在混合这些 MySQL API。他们混合。 mysqli_ + mysql_ = 没有爱。
  • 只需使用$bd = mysqli_connect($mysql_hostname, $mysql_user, $mysql_password, $mysql_database) 就可以了。您不需要将mysqli_select_dbmysqli 一起使用。你可以,但是通过使用所有 4 个参数,它会覆盖它host, username, password, db。只是不要将mysqli_select_db 与此建议结合使用。

标签: php mysql database session encryption


【解决方案1】:

我尚未对此进行测试,但如果您想要一个如何将其转换为 PDO 的示例

<?php
    //really basic connection wrapper class
    class DB{
        protected $_conn;

        public function __construct(){
            $mysql_hostname = "localhost";
            $mysql_user = "root";
            $mysql_password = "";
            $mysql_database = "web";
            //http://php.net/manual/en/pdo.construct.php
            $this->_conn = new PDO(
                'mysql:host=' . $mysql_hostname . ';' . 'dbname=' . $mysql_database,
                $mysql_user,
                $mysql_password
            );
        }

        public function getConn(){
            return $this->_conn;
        }
        /*
        $bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Can't connect to database");
        mysql_select_db($mysql_database, $bd) or die("Opps some thing went wrong");
        */
    }
?>

//any space or new lines here ( before the open PHP tag ) will prevent the redirect
<?php
ini_set('display_errors', 1); //turn on error reporting when developing
include("db.php");
session_start();
if($_SERVER["REQUEST_METHOD"] == "POST"){
    $DB = new DB();

    // username and password sent from Form
    $username = $_POST['username']; 
    $password = $_POST['password']; 
    $password = md5($password); // Encrypted Password - update this as others have stated
    $sql="SELECT id FROM admin WHERE username = ? and passcode = ?";
    //http://php.net/manual/en/pdo.prepare.php
    $stmt = $DB->getConn()->prepare($sql);
    //http://php.net/manual/en/pdostatement.execute.php
    $stmt->execute( array( $username, $password ) );
    //http://php.net/manual/en/pdostatement.fetchall.php
    $results = $stmt->fetchAll(PDO::FETCH_ASSOC);

    // If result matched $username and $password, table row must be 1 row
    if(count( $results ) ){
        //session_register("username"); this is deprecated 
        $_SESSION['login_user']=$username;

        header("location: welcome.php");
        exit(); //add exit here
    }else {
        $error="Your Login Name or Password is invalid";
    }
}
?>
<form action="login.php" method="post">
<label>UserName :</label>
<input type="text" name="username"/><br />
<label>Password :</label>
<input type="password" name="password"/><br/>
<input type="submit" value=" Login "/><br />
</form>

用于重定向问题。 不要使用 session_register(); How to fix the session_register() deprecated issue?

【讨论】:

  • 谢谢!登录成功,但它没有指向另一个页面,或者在这种情况下是welcome.php
  • 确保在标题('location ...)查看我的更新之前不输出任何内容。 session_register 自 php 5.3 起已弃用,这将导致输出并破坏重定向。
猜你喜欢
  • 2014-12-29
  • 2014-01-05
  • 2017-08-20
  • 2016-09-28
  • 2012-12-06
  • 2014-09-18
  • 1970-01-01
  • 2022-12-06
  • 2017-02-06
相关资源
最近更新 更多