Home >Database >Mysql Tutorial >How to connect to MySQL database in PHP
$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.
$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!