Home>Article>Backend Development> How to connect to database in PHP7
Recommended (free):PHP7
* Already in PHP7 Themysql
library is abolished, so you can only usemysqli
andPDO
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); ?>
query('select * from customers'); $data = $result->fetchAll(PDO::FETCH_ASSOC); // PDO::FETCH_ASSOC表示将对应结果集中的每一行作为一个由列名索引的数组返回 print_r($data);} catch (PDOException $error){ echo 'connect failed:'.$error->getMessage();} ?>
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!