Home  >  Article  >  Backend Development  >  Two ways to connect to myql database in php

Two ways to connect to myql database in php

autoload
autoloadOriginal
2021-05-06 09:27:335508browse

phpIn 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=&#39;localhost&#39;;
$dbname=&#39;grade&#39;;
$username=&#39;root&#39;;
$password=&#39;root123456&#39;;

$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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn