Home  >  Article  >  Database  >  How to connect to MySQL database in PHP

How to connect to MySQL database in PHP

王林
王林forward
2023-04-17 18:01:051682browse

mysqli Connect to the database

$servername="localhost";
$username="root";
$password="root";
$dbname="name1";
$link = mysqli_connect($servername, $username, $password, $dbname);
if (mysqli_connect_errno()) {
    printf("连接失败: %s\n", mysqli_connect_error());
    exit();
}
echo "连接成功".
"";

If the connection is successful, the connection success will be output. If the connection fails, the connection failure will be output.

pdo Connect to the database

$host='localhost';
$dbname='name1';
$username='root';
$password='root';
$pdo=new PDO("mysql:host=$host;dbname=$dbname",$username,$password);
echo "连接成功"."
";
?>

Same as above, if the connection is successful, the connection success will be output, if the connection fails, the connection failure will be output.

The above is the detailed content of How to connect to MySQL database in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete