current position:Home>How to rewrite the code of linking MySQL database?

How to rewrite the code of linking MySQL database?

2022-02-04 17:25:06 CSDN Q & A

// SQL server connection information$sql_details = array(    'user' => 'root',    'pass' => 'root',    'db'   => 'table_demo',    'host' => 'localhost');
    static function sql_connect ( $sql_details ) {        try {            $db = @new PDO(                "mysql:host={$sql_details['host']};dbname={$sql_details['db']}",                $sql_details['user'],                $sql_details['pass'],                array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION )            );        }        catch (PDOException $e) {            self::fatal(                "An error occurred while connecting to the database. ".                "The error reported by the server was: ".$e->getMessage()            );        }        return $db;    }

The above link SQL server Code of the database , How to write a link mysql What about the database ?




Take the answer :
static function sql_connect ( $sql_details ) {        try {            $db = @new PDO("mysql:host=".$sql_details['host'].";port=".$sql_details['port'].";dbname=".$sql_details['db'].";charset=".$sql_details['charSet'];, $sql_details['user'],$sql_details['pass'],array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ));        }        catch (PDOException $e) {            self::fatal(                "An error occurred while connecting to the database. ".                "The error reported by the server was: ".$e->getMessage()            );        }        return $db;    }
$sql_details = array(        'user' => 'root',        'pass' => 'root',        'db'   => 'table_demo',        'host' => 'localhost',        'port' => 3306,        'charSet' => 'utf8'    );

Have a try. , Make sure it's turned on PDO Link way , Is to php.ini In profile Write off extension=php_pdo_mysql.dll The semicolon in front , And restart the service



Other answers 2:

This is just a variable , Access the corresponding database link information , Change it to you mysql Corresponding username、password、database and host Just fine .


Other answers 3:

PDO An abstraction layer that provides access to data , No matter which database you use , You can use the same function ( Method ) To query and retrieve data . So change it MySQL Connection information for the server ( Database user name 、 password 、 port 、 Database name 、 The server IP Address ) That's all right. .

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/02/202202041725036035.html

Random recommended