How to connect to database in PHP7
Recommended (free): PHP7
* Already in PHP7 The mysql
library is abolished, so you can only use mysqli
and PDO
<?php$serve = 'localhost:3306';$username = 'root';$password = 'admin123';$dbname = 'examples';$mysqli = new Mysqli($serve,$username,$password,$dbname);if($mysqli->connect_error){
die('connect error:'.$mysqli->connect_errno);}$mysqli->set_charset('UTF-8'); // 设置数据库字符集$result = $mysqli->query('select * from customers');$data = $result->fetch_all(); // 从结果集中获取所有数据print_r($data);
?>
Copy after login
mysqliProcess-oriented style<?php$serve = 'localhost:3306';$username = 'root';$password = 'admin123';$dbname = 'examples';$mysqli = new Mysqli($serve,$username,$password,$dbname);if($mysqli->connect_error){ die('connect error:'.$mysqli->connect_errno);}$mysqli->set_charset('UTF-8'); // 设置数据库字符集$result = $mysqli->query('select * from customers');$data = $result->fetch_all(); // 从结果集中获取所有数据print_r($data); ?>
Copy after login
<?php$serve = 'localhost:3306';$username = 'root';$password = 'admin123';$dbname = 'examples';$link = mysqli_connect($serve,$username,$password,$dbname);mysqli_set_charset($link,'UTF-8'); // 设置数据库字符集$result = mysqli_query($link,'select * from customers');$data = mysqli_fetch_all($result); // 从结果集中获取所有数据print_r($data);
?>
Copy after login
PDO connection database<?php$serve = 'localhost:3306';$username = 'root';$password = 'admin123';$dbname = 'examples';$link = mysqli_connect($serve,$username,$password,$dbname);mysqli_set_charset($link,'UTF-8'); // 设置数据库字符集$result = mysqli_query($link,'select * from customers');$data = mysqli_fetch_all($result); // 从结果集中获取所有数据print_r($data); ?>
Copy after login
<?php$serve = 'mysql:host=localhost:3306;dbname=examples;charset=utf8';$username = 'root';$password = 'admin123';try{ // PDO连接数据库若错误则会抛出一个PDOException异常
$PDO = new PDO($serve,$username,$password);
$result = $PDO->query('select * from customers');
$data = $result->fetchAll(PDO::FETCH_ASSOC); // PDO::FETCH_ASSOC表示将对应结果集中的每一行作为一个由列名索引的数组返回
print_r($data);} catch (PDOException $error){
echo 'connect failed:'.$error->getMessage();}
?>
Copy after login
You can use PDO or mysqli to connect to mysql, but it is more recommended to use PDO to connect to the database, because PDO supports 12 different database drivers. mysqli only supports mysql, and PDO has higher performance<?php$serve = 'mysql:host=localhost:3306;dbname=examples;charset=utf8';$username = 'root';$password = 'admin123';try{ // PDO连接数据库若错误则会抛出一个PDOException异常 $PDO = new PDO($serve,$username,$password); $result = $PDO->query('select * from customers'); $data = $result->fetchAll(PDO::FETCH_ASSOC); // PDO::FETCH_ASSOC表示将对应结果集中的每一行作为一个由列名索引的数组返回 print_r($data);} catch (PDOException $error){ echo 'connect failed:'.$error->getMessage();} ?>
Copy after login
The above is the detailed content of How to connect to database in PHP7. For more information, please follow other related articles on the PHP Chinese website!
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

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago
By 尊渡假赌尊渡假赌尊渡假赌
Assassin's Creed Shadows: Seashell Riddle Solution
2 weeks ago
By DDD
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics
CakePHP Tutorial
1381
52

