Home >Backend Development >PHP Tutorial >How to connect to MySQL using PHP

How to connect to MySQL using PHP

青灯夜游
青灯夜游Original
2019-01-31 18:03:1822246browse

How to connect to MySQL using PHP: 1. Use PHP’s mysql_connect statement to connect; 2. Use PHP’s MySQLi statement to connect; 3. Use the PDO method to connect.

How to connect to MySQL using PHP

The operating environment of this article: Windows 7 system, Dell G3 computer, PHP version 7.1.

We can use PHP's mysql_connect statement, MySQLi statement or PDO to connect to MySQL. Here is a brief introduction in this article. I hope it will be helpful to everyone.

Before we can access the data in the MySQL database, we need to be able to connect to the MySQL server. Let's take a look at how to connect to MySQL.

Use PHP's mysql_connect statement

PHP provides the mysql_connect function to open a database connection. Let's take a look at how to implement it:

Step 1. Use the following PHP code to connect to MySQL and select the database.

<?php
    $con = mysql_connect(&#39;localhost&#39;,&#39; username &#39;,&#39; password &#39;);
    $db_selected = mysql_select_db("dbname", $con);
?>

Note: You need to replace username and password with your own MySQL account name and password; and change dbname to your own defined database name.

Step 2. After connecting to MySQL and selecting a database, you can run SQL queries and perform other operations. For example, the following PHP code runs a SQL query that extracts the name field from the employees table and stores the result in the $result variable:

<?php
    $result = mysqli_query($db_selected,&#39;SELECT name FROM employees&#39;);
?>

MySQLi statement using PHP

Method 1, MySQLi object-oriented

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB"
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检查连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
} 
echo "连接成功";
?>

Method 2, MySQLi program

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB"
// 创建连接
$conn = mysqli_connect($servername, $username, $password,$dbname);
// 检查连接
if (!$conn) {
    die("连接失败: " . mysqli_connect_error());
}
echo "连接成功";
?>

Using PDO

<?php
$servername = "localhost";
$username = "username";
$password = "password";
try {
    $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
    // 将PDO错误模式设置为异常
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "连接成功"; 
}
catch(PDOException $e){
    echo "连接失败:".$e->getMessage();
}
?>

The above is the entire content of this article, I hope it will be helpful to everyone's learning. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of How to connect to MySQL using 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