


Detailed explanation of PHP7 database connection and addition, deletion, query and modification (mysqli method)
## Use the mysqli method to achieve the following functions (php7):
1. Connect to the MySQL database server; 2. Create a database named test;
3. Create a data table named "testTable" in the database. The data table contains at least three fields. Field names, types and attributes are customized;
4. Insert three records into the database and query all data in the data table;
5. Modify one of the records and query all data in the data table;
6. Delete one of the records and query all the data in the data table;
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" > <title>mysqli方法实现连接数据库,及增删查改</title> </head> <body> <?php $con = @mysqli_connect("localhost","root","15118595615"); if($con){ echo "数据库连接成功!</br>"; } else{ echo "数据库连接失败!</br>"; } $sql="CREATE DATABASE test"; if (mysqli_query($con,$sql)){ echo "数据库创建成功!</br>"; }else{ echo "数据库创建失败!</br>".mysqli_error($con)."</br>"; } mysqli_select_db($con,"test"); $table="CREATE TABLE testTable( student_id int(11) auto_increment primary key, student_no char(10) not null unique, student_name char(20) not null)"; if(mysqli_query($con,$table)){ echo "数据表创建成功!</br>"; } else{ echo "数据表创建失败!</br>".mysqli_error($con)."</br>"; } $mysqli=new mysqli("localhost","root","15118595615","test"); $query="select * from testTable"; $insertdatas=mysqli_query($con,"insert into testTable(student_id,student_no,student_name) values('null','20170001','张三')"); mysqli_free_result($insertdatas); $insertdatas=mysqli_query($con,"insert into testTable(student_id,student_no,student_name) values('null','20170002','李四')"); mysqli_free_result($insertdatas); $insertdatas=mysqli_query($con,"insert into testTable(student_id,student_no,student_name) values('null','20170003','王五')"); if($insertdatas){ echo "数据插入成功!</br>"; $result=$mysqli->query($query); foreach($result as $row){ echo $row["student_id"].'  '; echo $row["student_no"].'  '; echo $row["student_name"]."</br>"; } } else{ echo "数据插入失败!</br>".mysqli_error($con)."</br>"; } mysqli_free_result($insertdatas); $up=mysqli_query($con,"update testTable set student_no='20180001' where student_name='张三'"); if($up){ echo "数据更新成功!</br>"; $result=$mysqli->query($query); foreach($result as $row){ echo $row["student_id"].'  '; echo $row["student_no"].'  '; echo $row["student_name"]."</br>"; } } else{ echo "数据更新失败!</br>".mysqli_error($con)."</br>"; } mysqli_free_result($up); $del=mysqli_query($con,"delete from testTable where student_name='李四'"); if($del){ echo "数据删除成功!</br>"; $result=$mysqli->query($query); foreach($result as $row){ echo $row["student_id"].'  '; echo $row["student_no"].'  '; echo $row["student_name"]."</br>"; } } else{ echo "数据删除失败!</br>".mysqli_error($con)."</br>"; } mysqli_free_result($del); mysqli_close($con); ?> </body> </html>
When writing code Pay attention to some differences between PHP7 and PHP5:
1. PHP7 needs to replace PHP5’s
mysql() with mysqli(); 2. PHP7’s query statement must Written as
mysqli(
c
o
n
n
e
c
t
, ,
Connect,
connect,sql), the writing method of PHP5 is opposite to that of PHP7mysql(
s
q q
l
,
sql,
sql,connect);
Warm reminder: Be sure to use the mysqli_free_result() function to release resources after each query! Otherwise, an error will be reported and the next query statement cannot be executed! I took a lot of detours when I was a beginner, and I have learned hard lessons. I hope it can help my beginner friends to avoid detours!
用mysqli方法 实现以下功能(php7):
1、连接MySQL数据库服务器;
2、创建一个名为test的数据库;
3、在该数据库内创建一个名为“testTable”的数据表,数据表至少包含三个字段,字段名字、类型和属性自定;
4、为该数据库插入三条记录,并查询该数据表的所有数据;
5、修改其中的一条记录,并查询该数据表的所有数据;
6、删除其中的一条记录,并查询该数据表的所有数据;
<!DOCTYPE html><html><head><meta charset="UTF-8" ><title>mysqli方法实现连接数据库,及增删查改</title></head><body><?php $con = @mysqli_connect("localhost","root","15118595615"); if($con){ echo "数据库连接成功!</br>"; } else{ echo "数据库连接失败!</br>"; } $sql="CREATE DATABASE test"; if (mysqli_query($con,$sql)){ echo "数据库创建成功!</br>"; }else{ echo "数据库创建失败!</br>".mysqli_error($con)."</br>"; } mysqli_select_db($con,"test"); $table="CREATE TABLE testTable( student_id int(11) auto_increment primary key, student_no char(10) not null unique, student_name char(20) not null)"; if(mysqli_query($con,$table)){ echo "数据表创建成功!</br>"; } else{ echo "数据表创建失败!</br>".mysqli_error($con)."</br>"; } $mysqli=new mysqli("localhost","root","15118595615","test"); $query="select * from testTable"; $insertdatas=mysqli_query($con,"insert into testTable(student_id,student_no,student_name) values('null','20170001','张三')"); mysqli_free_result($insertdatas); $insertdatas=mysqli_query($con,"insert into testTable(student_id,student_no,student_name) values('null','20170002','李四')"); mysqli_free_result($insertdatas); $insertdatas=mysqli_query($con,"insert into testTable(student_id,student_no,student_name) values('null','20170003','王五')"); if($insertdatas){ echo "数据插入成功!</br>"; $result=$mysqli->query($query); foreach($result as $row){ echo $row["student_id"].'  '; echo $row["student_no"].'  '; echo $row["student_name"]."</br>"; } } else{ echo "数据插入失败!</br>".mysqli_error($con)."</br>"; } mysqli_free_result($insertdatas); $up=mysqli_query($con,"update testTable set student_no='20180001' where student_name='张三'"); if($up){ echo "数据更新成功!</br>"; $result=$mysqli->query($query); foreach($result as $row){ echo $row["student_id"].'  '; echo $row["student_no"].'  '; echo $row["student_name"]."</br>"; } } else{ echo "数据更新失败!</br>".mysqli_error($con)."</br>"; } mysqli_free_result($up); $del=mysqli_query($con,"delete from testTable where student_name='李四'"); if($del){ echo "数据删除成功!</br>"; $result=$mysqli->query($query); foreach($result as $row){ echo $row["student_id"].'  '; echo $row["student_no"].'  '; echo $row["student_name"]."</br>"; } } else{ echo "数据删除失败!</br>".mysqli_error($con)."</br>"; } mysqli_free_result($del); mysqli_close($con); ?></body></html>
最终效果如下:
写代码的时候要注意PHP7和PHP5的一些差别:
1、PHP7要将PHP5的mysql()换成mysqli();
2、PHP7的查询语句要写成mysqli(
c
o
n
n
e
c
t
,
connect,
connect,sql),PHP5的写法和PHP7的相反mysql(
s
q
l
,
sql,
sql,connect);
温馨提示:
每次查询完之后一定要用mysqli_free_result()函数释放资源!不然会报错,无法执行下一条查询语句!初学的时候走了不少弯路,血的教训,希望能给初学的朋友帮助,少走弯路!
The above is the detailed content of Detailed explanation of PHP7 database connection and addition, deletion, query and modification (mysqli method). 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



When writing web applications using PHP, a MySQL database is often used to store data. PHP provides a way to interact with the MySQL database called MySQLi. However, sometimes when using MySQLi, you will encounter an error message, as shown below: PHPFatalerror:Calltoundefinedfunctionmysqli_connect() This error message means that PHP cannot find my

Solution to php unable to connect to mysqli: 1. Open the "php.ini" file; 2. Find "mysqli.reconnect"; 3. Change "mysqli.reconnect = OFF" to "mysqli.reconnect = on".

PDOPDO is an object-oriented database access abstraction layer that provides a unified interface for PHP, allowing you to use the same code to interact with different databases (such as Mysql, postgresql, oracle). PDO hides the complexity of underlying database connections and simplifies database operations. Advantages and Disadvantages Advantages: Unified interface, supports multiple databases, simplifies database operations, reduces development difficulty, provides prepared statements, improves security, supports transaction processing Disadvantages: performance may be slightly lower than native extensions, relies on external libraries, may increase overhead, demo code uses PDO Connect to mysql database: $db=newPDO("mysql:host=localhost;dbnam

In php5, we can use the fsockopen() function to detect the TCP port. This function can be used to open a network connection and perform some network communication. But in php7, the fsockopen() function may encounter some problems, such as being unable to open the port, unable to connect to the server, etc. In order to solve this problem, we can use the socket_create() function and socket_connect() function to detect the TCP port.

The running file of mysql is mysqld; mysqld is an executable file, which represents the Mysql server program. Executing this file can directly start a server process; and mysqld_safe is a startup script, which will indirectly call mysqld and also start a monitor. process.

If you encounter the following error message when using PHP to connect to a MySQL database: PHPWarning:mysqli_connect():(HY000/2002):Connectionrefused, then you can try to solve this problem by following the steps below. To confirm whether the MySQL service is running normally, you should first check whether the MySQL service is running normally. If the service is not running or fails to start, it may cause a connection refused error. you can

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.

How to install the mongo extension in php7.0: 1. Create the mongodb user group and user; 2. Download the mongodb source code package and place the source code package in the "/usr/local/src/" directory; 3. Enter "src/" directory; 4. Unzip the source code package; 5. Create the mongodb file directory; 6. Copy the files to the "mongodb/" directory; 7. Create the mongodb configuration file and modify the configuration.
