Home > Database > Mysql Tutorial > How to connect to MySQL database in PHP

How to connect to MySQL database in PHP

王林
Release: 2023-04-17 18:01:05
forward
1743 people have browsed it

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 "连接成功".
"";
Copy after login

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 "连接成功"."
";
?>
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template