Home>Article>Backend Development> How can PHP7 connect to the database

How can PHP7 connect to the database

醉折花枝作酒筹
醉折花枝作酒筹 forward
2021-05-26 09:20:24 2158browse

This article will introduce to you how to connect to the database in PHP7. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

How can PHP7 connect to the database

* Themysqllibrary has been abolished in PHP7, so you can only usemysqliandPDO

mysqli object-oriented style

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); ?>

mysqli process-oriented style

PDO connects to the database

query('select * from customers'); $data = $result->fetchAll(PDO::FETCH_ASSOC); // PDO::FETCH_ASSOC表示将对应结果集中的每一行作为一个由列名索引的数组返回 print_r($data); } catch (PDOException $error){ echo 'connect failed:'.$error->getMessage(); } ?>

Use PDO or mysqli You can 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

Recommended learning:php video tutorial

The above is the detailed content of How can PHP7 connect to the database. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete