Home > Daily Programming > PHP Knowledge > How to connect to Mysql database with PHP

How to connect to Mysql database with PHP

藏色散人
Release: 2021-04-16 14:24:16
Original
67648 people have browsed it

How to connect PHP to Mysql database: 1. Use mysqli to connect to the Mysql database, code such as "new mysqli($host,$user,$password,$dbName);"; 2. Use PDO to connect to the database, code Such as "new PDO(...)".

The operating environment of this article: Windows7 system, Dell G3 computer, PHP7.1.

PHP connects to mysql database is a skill that PHP novices must master. As long as you master PHP operations such as adding, deleting, modifying, and querying the database, you can write some simple and common procedures. Such as message form, news page, etc. This article mainly introduces in detail two common methods for connecting PHP to Mysql database.

Below we will give you a detailed introduction to two methods of PHP connecting to mysql database through specific code examples.

There are two methods for mysqli to connect to the database and pdo to connect to the database.

The first method: use mysqli to connect to the mysql database

The code example is as follows:

<?php
$host=&#39;127.0.0.1&#39;;
$user=&#39;root&#39;;
$password=&#39;root&#39;;
$dbName=&#39;php&#39;;
$link=new mysqli($host,$user,$password,$dbName);
if ($link->connect_error){
    die("连接失败:".$link->connect_error);
}
    $sql="select * from admins";
    $res=$link->query($sql);
    $data=$res->fetch_all();
    var_dump($data);
Copy after login

In the above code, we need to first create some requirements Variables used, such as database user name, database name and password, etc. Then we used object-oriented method to connect to the database named php. Then use the if conditional statement and connect-error method to determine whether the PHP connection to the database is successful.

After a series of connection operations, we create a sql statement to query and verify the data table.

Here we first log in to phpmyadmin to see if the php database exists. From the picture below, we can know that the php database exists.

How to connect to Mysql database with PHP

Finally accessed through the browser, the result is as follows:

How to connect to Mysql database with PHP

As can be known from the picture, We successfully connected to the php database and were able to query the data table information.

Second method: Use PDO to connect to the database

The code example is as follows:

<?php
$host=&#39;127.0.0.1&#39;;
$user=&#39;root&#39;;
$password=&#39;root&#39;;
$dbName=&#39;php&#39;;
$pdo=new PDO("mysql:host=$host;dbname=$dbName",$user,$password);
$sql="select * from admins";
$data=$pdo->query($sql)->fetch();
var_dump($data);
Copy after login

Access through the browser, the result is as shown in the figure:

How to connect to Mysql database with PHP

The above is a detailed explanation of the two common methods of PHP connecting to the database to query data. For more related tutorials, please visit the php Chinese website mysql video tutorial. Welcome to refer to and learn!

The above is the detailed content of How to connect to Mysql database with 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