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='127.0.0.1'; $user='root'; $password='root'; $dbName='php'; $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);
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.
Finally accessed through the browser, the result is as follows:
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='127.0.0.1'; $user='root'; $password='root'; $dbName='php'; $pdo=new PDO("mysql:host=$host;dbname=$dbName",$user,$password); $sql="select * from admins"; $data=$pdo->query($sql)->fetch(); var_dump($data);
Access through the browser, the result is as shown in the figure:
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver Mac version
Visual web development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
