Two ways to connect to myql database in php

autoload
Release: 2023-04-09 22:46:02
Original
5635 people have browsed it

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

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

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!

Related labels:
source:php.cn
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