【问题标题】:How to use mysqli in PHP in this code如何在这段代码中在 PHP 中使用 mysqli
【发布时间】:2016-07-14 17:49:50
【问题描述】:

目前我的代码是用来连接我的数据库的。

<?php

/**
* A class file to connect to database
*/
class DB_CONNECT {

// constructor
function __construct() {
    // connecting to database
    $this->connect();
}

// destructor
function __destruct() {
    // closing db connection
    $this->close();
}

/**
 * Function to connect with database
 */
function connect() {
    // import database connection variables
    require_once __DIR__ . '/db_config.php';

    // Connecting to mysql database
    $con = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysql_error());

    // Selecing database
    $db = mysql_select_db(DB_DATABASE) or die(mysql_error()) or die(mysql_error());

    // returing connection cursor
    return $con;
}

/**
 * Function to close db connection
 */
function close() {
    // closing db connection
    mysql_close();
}

}

?>

db_config.php 是

<?php

define('DB_USER', "root"); // db user 
define('DB_PASSWORD', ""); // db password (mention your db password here)
define('DB_DATABASE', "leading10"); // database name
define('DB_SERVER', "localhost"); // db server
?>

在某处使用上述代码时,它会显示使用 mysqli 或 PDO 的警告 我关注了许多 stackoverflow 问题,但仍然无法成功。 我知道的一种方法是在 mysql_connect() 之前使用 @ 符号,但将来可能无法使用。 任何帮助表示赞赏

【问题讨论】:

    标签: php mysql mysqli pdo mysql-connect


    【解决方案1】:

    正如其他人已经写的那样,您应该使用mysqli_* 函数。但这不是你的问题。

    问题是您将连接分配给 $con 这是一个局部变量,因此 - 一旦 connect() 退出 - 值就会丢失。

    此代码有效:

    class DB_CONNECT
    {
        private $connection;
        private $database;
    
        function __construct() {
            $this->connect();
        }
    
        function __destruct() {
            $this->close();
        }
    
        function connect() {
            $this->connection = mysqli_connect( DB_SERVER, DB_USER, DB_PASSWORD ) or die(mysql_error());
            $this->database = mysqli_select_db( DB_DATABASE ) or die(mysql_error());
        }
    
        function close() {
            mysqli_close( $this->connection );
        }
    }
    

    注意:

    在您的代码中,一行中有两次or die(...)

    【讨论】:

      【解决方案2】:

      使用它来关闭错误报告

      写在页面顶部

      error_reporting(E_ERROR | E_WARNING | E_PARSE);
      

      这里是更多信息的链接

      http://www.w3schools.com/php/func_error_reporting.asp

      【讨论】:

        【解决方案3】:

        因为你用的是mysql,所以没用,已经折旧,把mysql全部改成mysqli。这是您的代码的编辑版本。

         <?php
        
        /**
        * A class file to connect to database
        */
        class DB_CONNECT {
        
        // constructor
        function __construct() {
            // connecting to database
            $this->connect();
        }
        
        // destructor
        function __destruct() {
            // closing db connection
            $this->close();
        }
        
        /**
         * Function to connect with database
         */
        function connect() {
            // import database connection variables
            require_once __DIR__ . '/db_config.php';
        
            // Connecting to mysql database
            $con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysqli_error());
        
            // Selecing database
            $db = mysqli_select_db(DB_DATABASE) or die(mysqli_error());
        
            // returing connection cursor
            return $con;
        }
        
        /**
         * Function to close db connection
         */
        function close() {
            // closing db connection
            mysqli_close();
        }
        
        }
        
        ?>
        

        【讨论】:

        • 我相信这不是原因。
        • 你试过了吗?你是第一个没有尝试就投反对票的人。
        • 是的,我收到了警告,但我得到了一个有效的连接。正如我所写,这不是问题。我发布了该问题的正确答案。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-06-16
        • 1970-01-01
        • 2023-04-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-31
        相关资源
        最近更新 更多