How to connect to the database in php7

(*-*)浩
Release: 2023-02-26 11:28:01
Original
3026 people have browsed it

The methods to use native PHP to connect to MySQL include the MySQL library, MySQLi library and PDO. Since PHP 7 has abolished the MySQL library, it is recommended to use MySQLi and PDO.

How to connect to the database in php7

There are two styles for connecting to MySQLi:

Object-oriented style ( Recommended) (Recommended learning:PHP video tutorial)

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

Procedural style

Copy after login

PDO connection 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(); } ?>
Copy after login

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

Related labels:
source:php.cn
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!