What is the difference between mysql and mysqli in php
Difference: mysqli is a permanent connection function, while mysql is a non-persistent connection function. Mysql will open a connection process every time it is connected; mysqli always uses the same connection process, which can greatly reduce the pressure on the server side.
The operating environment of this tutorial: windows7 system, PHP7.1&&mysql8 version, DELL G3 computer
1. mysql and Related concepts of mysqli:
1. Mysql and mysqli are both function sets in PHP and have little connection with the mysql database.
2. Before the php5 version, the mysql function of php was generally used to drive the mysql database. For example, the mysql_query() function is process-oriented. 3. After the php5 version, the mysqli function has been added. In a sense, it is an enhanced version of the mysql system function, which is more stable, efficient and secure. Corresponding to mysql_query() is mysqli_query(), which is object-oriented and uses objects to operate and drive the mysql database
2. The difference between mysql and mysqli:
mysqli is a permanent connection function, while mysql is a non-persistent connection function.
mysql connection: Every time it is used for the second time, a new process will be reopened.
mysqli connection: always use the same process.
Benefits: This can greatly reduce the pressure on the server side.
Mysqli encapsulates some advanced operations such as transactions, and also encapsulates many available methods in the DB operation process.
3. Usage of mysql and mysqli:
1: mysql (process mode):
$conn = mysql_connect('localhost', 'user', 'password'); //连接mysql数据库 mysql_select_db('data_base'); //选择数据库 $result = mysql_query('select * from data_base');//第二个可选参数,指定打开的连接 $row = mysql_fetch_row( $result ) ) //只取一行数据 echo $row[0]; //输出第一个字段的值
PS: mysqli operates in a procedural manner. Some functions must specify resources, such as mysqli_query (resource identifier, SQL statement), and the parameter of the resource identifier is placed in front, while mysql_query (SQL statement, 'resource identifier' ) is optional, and the default value is the last opened connection or resource.
2. mysqli (object mode):
$conn = new mysqli('localhost', 'user', 'password','data_base'); //要使用new操作符,最后一个参数是直接指定数据库 //假如构造时候不指定,那下一句需要$conn -> select_db('data_base')实现 $result = $conn -> query( 'select * from data_base' ); $row = result -> fetch_row(); //取一行数据 echo row[0]; //输出第一个字段的值
Use new mysqli('localhost', usenamer', 'password', 'databasename');
An error will be reported and the prompt is as follows:
Fatal error: Class 'mysqli' not found in ...
Generally, mysqli is not enabled because the mysqli class is not enabled by default. Under win, you need to change php.ini and remove the ";" in front of php_mysqli.dll. Under Linux Mysqli needs to be compiled into it.
4. mysql_connect() and mysqli_connect()
1. Using mysqli, you can pass the database name as a parameter to mysqli_connect() function, can also be passed to the constructor of mysqli;
2. If the mysqli_query() or mysqli object query query() method is called, the connection identifier is required.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What is the difference between mysql and mysqli in php. 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

AI Hentai Generator
Generate AI Hentai for free.

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



The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

PHP is not dying, but constantly adapting and evolving. 1) PHP has undergone multiple version iterations since 1994 to adapt to new technology trends. 2) It is currently widely used in e-commerce, content management systems and other fields. 3) PHP8 introduces JIT compiler and other functions to improve performance and modernization. 4) Use OPcache and follow PSR-12 standards to optimize performance and code quality.

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

The methods for viewing SQL database errors are: 1. View error messages directly; 2. Use SHOW ERRORS and SHOW WARNINGS commands; 3. Access the error log; 4. Use error codes to find the cause of the error; 5. Check the database connection and query syntax; 6. Use debugging tools.
