简体   繁体   中英

How would I connect to a mysql database using PHP?

I have read that PHP is obsoleting the MySql functions.

How will we connect to MySql database?

Where did you hear that? I think it would be a great idea, but it's news to me.

It's better to use new mysqli('hostname', 'username', 'password', 'database')

Or (even better) use new PDO('mysql:dbname=database;host=hostname', 'username', 'password')

使用新的MySQLi类。

The new, better way of doing it is to use PDO. You can find pretty of examples on the web if you Google for "php mysql pdo" , here is one .

Using PDO (PHP Data Objects)

example.

$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';

try {
    $dbh = new PDO($dsn, $user, $password);
}
catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM