search
HomeBackend DevelopmentPHP7How to connect to database in PHP7

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

##mysqli object-oriented style

<?php$serve = &#39;localhost:3306&#39;;$username = &#39;root&#39;;$password = &#39;admin123&#39;;$dbname = &#39;examples&#39;;$mysqli = new Mysqli($serve,$username,$password,$dbname);if($mysqli->connect_error){
	die(&#39;connect error:&#39;.$mysqli->connect_errno);}$mysqli->set_charset(&#39;UTF-8&#39;); // 设置数据库字符集$result = $mysqli->query(&#39;select * from customers&#39;);$data = $result->fetch_all(); // 从结果集中获取所有数据print_r($data);

 ?>

mysqliProcess-oriented style

<?php$serve = &#39;localhost:3306&#39;;$username = &#39;root&#39;;$password = &#39;admin123&#39;;$dbname = &#39;examples&#39;;$link = mysqli_connect($serve,$username,$password,$dbname);mysqli_set_charset($link,&#39;UTF-8&#39;); // 设置数据库字符集$result = mysqli_query($link,&#39;select * from customers&#39;);$data = mysqli_fetch_all($result); // 从结果集中获取所有数据print_r($data);

 ?>

PDO connection database

<?php$serve = &#39;mysql:host=localhost:3306;dbname=examples;charset=utf8&#39;;$username = &#39;root&#39;;$password = &#39;admin123&#39;;try{ // PDO连接数据库若错误则会抛出一个PDOException异常
	$PDO = new PDO($serve,$username,$password);
	$result = $PDO->query(&#39;select * from customers&#39;);
	$data = $result->fetchAll(PDO::FETCH_ASSOC); // PDO::FETCH_ASSOC表示将对应结果集中的每一行作为一个由列名索引的数组返回
	print_r($data);} catch (PDOException $error){
	echo &#39;connect failed:&#39;.$error->getMessage();}

 ?>

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

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
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.