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);">
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP development practice: Use PHPMailer to send emails to users in the MySQL database Introduction: In the construction of the modern Internet, email is an important communication tool. Whether it is user registration, password reset, or order confirmation in e-commerce, sending emails is an essential function. This article will introduce how to use PHPMailer to send emails and save the email information to the user information table in the MySQL database. 1. Install the PHPMailer library PHPMailer is

To resolve the plugin not showing installed issue in PHP 7.0: Check the plugin configuration and enable the plugin. Restart PHP to apply configuration changes. Check the plugin file permissions to make sure they are correct. Install missing dependencies to ensure the plugin functions properly. If all other steps fail, rebuild PHP. Other possible causes include incompatible plugin versions, loading the wrong version, or PHP configuration issues.

As the amount of data continues to increase, database performance has become an increasingly important issue. Hot and cold data separation processing is an effective solution that can separate hot data and cold data, thereby improving system performance and efficiency. This article will introduce how to use Go language and MySQL database to separate hot and cold data. 1. What is hot and cold data separation processing? Hot and cold data separation processing is a way of classifying hot data and cold data. Hot data refers to data with high access frequency and high performance requirements. Cold data

Common solutions for PHP server environments include ensuring that the correct PHP version is installed and that relevant files have been copied to the module directory. Disable SELinux temporarily or permanently. Check and configure PHP.ini to ensure that necessary extensions have been added and set up correctly. Start or restart the PHP-FPM service. Check the DNS settings for resolution issues.

To what extent can I develop MySQL database skills to be successfully employed? With the rapid development of the information age, database management systems have become an indispensable and important component in all walks of life. As a commonly used relational database management system, MySQL has a wide range of application fields and employment opportunities. So, to what extent do MySQL database skills need to be developed to be successfully employed? First of all, mastering the basic principles and basic knowledge of MySQL is the most basic requirement. MySQL is an open source relational database management

How to use MySQL database for time series analysis? Time series data refers to a collection of data arranged in time order, which has temporal continuity and correlation. Time series analysis is an important data analysis method that can be used to predict future trends, discover cyclical changes, detect outliers, etc. In this article, we will introduce how to use a MySQL database for time series analysis, along with code examples. Create a data table First, we need to create a data table to store time series data. Suppose we want to analyze the number

How to use MySQL database for image processing? MySQL is a powerful relational database management system. In addition to storing and managing data, it can also be used for image processing. This article will introduce how to use a MySQL database for image processing and provide some code examples. Before you begin, make sure you have installed a MySQL database and are familiar with basic SQL statements. Create a database table First, create a new database table to store the image data. The structure of the table can be as follows

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...
