Home > Article > Backend Development > Explain how to connect php7 to mysql database
Recommended (free): PHP7
## Users of PHP 5 can use mysql extension, mysqli and PDO_MYSQL. PHP 7 removed the mysql extension, leaving only the latter two options. This document explains the terminology of each API, helping us how to use the API and understand related API information. PHP provides three different APIs to connect to the mysql database. The sample code below shows 3 different ways to connect to the mysql database./* * mysqli * 数据库地址,登陆账号,密码,数据库名称 */ $mysqli = new mysqli("localhost", "root", "", "student"); $sql = "SELECT * FROM tb_user"; $result = $mysqli->query($sql); $row = $result->fetch_assoc(); // 从结果集中取得一行作为关联数组 echo $row["password"]; /* free result set */ $result->free(); /* close connection */ $mysqli->close();
/* * 第一个参数是mysql:host,第二是dbname,第三个账户名,第四个密码 */ try { $pdo = new PDO("mysql:host=localhost;dbname=student", "root", ""); } catch (PDOException $e) { echo 'Connection failed: ' . $e->getMessage(); } $sql = "select * from tb_user"; echo $sql . "<BR>"; $pdo->query('set names utf8;'); $result = $pdo->query($sql); $rows = $result->fetchAll(); foreach ($rows as $row) { $username = $row[1]; $pwd = $row[2]; echo $username; }
We recommend using mysqli or PDO_Mysql extension. It is not recommended to use the old mysql extension in new development because it is no longer used in PHP5.5.0 and Removed in PHP7.0.
It is important to set the encoding, it is utf8 instead of uft-8
$conn->set_charset("utf8");或者这样也可以$conn->query("set names utf8;");<pre style="font-family: 'DejaVu Sans Mono'; font-size: 15pt; background-color: rgb(255, 255, 255);">Concept:
Queries with cache and without cache
Queries use cached queries by default. This means that the query results are immediately sent from the MySQL server to PHP and then stored in the PHP parser memory. This allows additional operations like counting rows, moving or searching the current result pointer. It also allows further queries on the same connection and result set. The downside of caching mode is that large result sets may require large amounts of memory, which is occupied until the result set is cleared or freed, which is done automatically at the end of the request. The term stored results is used to indicate caching mode, where all result sets are saved at once.
Mysql query without cache is executed and a resource is returned immediately. The data is waiting for the mysql server to be connected and obtained. This uses less memory on the php side, but increases the load on the server. Until all result sets have been retrieved from the server and no queries have been sent over the same connection. Queries without caching are also known as consuming results.
It can be seen from these characteristics that cached queries are used when you only want to get a limited result set or know the number of rows in the returned result set before reading the result set. The uncached query mode is used when you want to return large amounts of data.
Because the default is cached query mode, the following example will verify how to execute the query API without cache.
<?php $mysqli = new mysqli("localhost", "my_user", "my_password", "world"); $uresult = $mysqli->query("SELECT Name FROM City", MYSQLI_USE_RESULT); if ($uresult) { while ($row = $uresult->fetch_assoc()) { echo $row['Name'] . PHP_EOL; } } $uresult->close(); ?>
The above is the detailed content of Explain how to connect php7 to mysql database. For more information, please follow other related articles on the PHP Chinese website!