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

How to connect to Mysql database with PHP

藏色散人
藏色散人 Original
2018-09-12 14:34:11 67265browse

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 databaseis 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 ofPHP connecting to mysql databasethrough 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:

connect_error){ die("连接失败:".$link->connect_error); } $sql="select * from admins"; $res=$link->query($sql); $data=$res->fetch_all(); var_dump($data);

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 usedobject-oriented methodto connect to the database named php. Then use the if conditional statement andconnect-error methodto 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:

query($sql)->fetch(); var_dump($data);

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

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