php
In the actual development process, it is often necessary to connect to the database. With the update of the php
version, two methods of connecting to the mysql
database are currently enabled by default. There is nothing better than mysqli
and pdo
. This article will take you to take a look.
1.pdo connects to the database
<?php $host='localhost'; $dbname='grade'; $username='root'; $password='root123456'; $pdo=new PDO("mysql:host=$host;dbname=$dbname",$username,$password); echo "连接成功"."<br>"; ?>
输出:连接成功
2.mysqli connects to the database
<?php $servername="localhost"; $username="root"; $password="root123456"; $dbname="grade"; $link = mysqli_connect($servername, $username, $password, $dbname); if (mysqli_connect_errno()) { printf("连接失败: %s\n", mysqli_connect_error()); exit(); } echo "连接成功"."<br>";
输出:连接成功
Recommended: 《2021 PHP interview questions summary (collection)》《php video tutorial》
The above is the detailed content of Two ways to connect to myql database in php. For more information, please follow other related articles on the PHP Chinese website!