Native PHP operation database addition, deletion, modification and query
Step one: Create a database and create a data table in the database. Of course, there can be many data tables in a database. Here I will create a table to store the students' personal names and grades.
Recommended mysql video tutorials: "mysql tutorial"
Idea: Connect to the server—>Create database—>Connect to database—>Create data Table
Script: Create database and data table
<?php header("Content-type:text/html;charset=utf-8"); // 创建连接 $conn=mysql_connect('localhost','root','');//三个参数分别对应服务器名,账号,密码 // 检测连接 if (!$conn) { die("连接服务器失败: " . mysql_connect_error());//连接服务器失败退出程序 } // 创建数据库命名为studentinfo $sql_database = "CREATE DATABASE studentinfo"; if (mysql_query($sql_database,$conn)) { echo "数据库创建成功</br>"; } else { echo "数据库创建失败: " . mysql_error()."</br>"; } //连接数据库studentinfo $sele=mysql_select_db( 'studentinfo' ); if(!$sele){ die("连接数据库失败: ".mysql_error());//连接数据库失败退出程序 } // 创建数据表命名为student,主键为id(不为空整型),变量名为name(255位不为空字符串),变量名为chinese(4位不为空整型) // 变量名为english(4位不为空整型),变量名为math(4位不为空整型) $sql_table = "CREATE TABLE student( ". "id INT NOT NULL AUTO_INCREMENT, ". "name CHAR(255) NOT NULL, ". "chinese INT(4) NOT NULL, ". "english INT(4) NOT NULL, ". "math INT(4) NOT NULL, ". "PRIMARY KEY ( id )); "; $retval = mysql_query( $sql_table, $conn ); if(! $retval ){ echo '数据表创建失败: ' . mysql_error()."</br>"; }else{ echo "数据表创建成功</br>"; } mysql_query('set names utf8'); mysql_close($conn);//关闭连接 ?>
Now you can see the new database studentinfo and data table student
Step 2: Add student information data (increase) in the student data table of the studentinfo database
Idea: Connect to the server->Connect to the database->Insert specified data into the data table
Note: Because the previous PHP has already created a server connection and connected to the database, the following code omits the connection establishment part and writes function statements directly.
function addtabel_data(){ //多维数组 $datas=array( array("name"=>"测试猫","chinese"=>100,"english"=>100,"math"=>100), array("name"=>"测试狗","chinese"=>99,"english"=>99,"math"=>99), array("name"=>"测试虎","chinese"=>98,"english"=>98,"math"=>98) ); for($i=0;$i<count($datas);$i++){ $name=$datas[$i]["name"]; $chinese=$datas[$i]["chinese"]; $english=$datas[$i]["english"]; $math=$datas[$i]["math"]; //多维数组数据逐条插入student表 mysql_query("insert into student(name,chinese,english,math) values ('$name',$chinese,$english,$math)"); } $res=mysql_affected_rows();//返回影响行 if($res>0){ echo "添加数据成功</br>"; }else{ echo "添加数据失败</br>"; } } addtabel_data();//调用
When running php, I found that adding data failed. Why? Because a string with Chinese characters is passed in the name, and the name sorting rule defined in the student table is not utf-8? ? ?
It’s okay. We can modify the sorting rules with one click. After modifying it ourselves
, run it again. The data is added successfully and It is found that there is data in the table
Step 3: Query one or more specified pieces of information in the student table of the studentinfo database according to the query conditions (check)
Idea: Connect to the server -> Connect to the database -> Query the data table data according to the conditions
function selecttable_data($name){ $res=mysql_query("select * from student where name='$name'");//根据name来查询student数据 // $res=mysql_query("select * from student where name='$name' and chinese='$chinese'");//多条件查询连接符and // $res=mysql_query("select * from student");//查询student表里所有数据 // $res=mysql_query("select * from student limit 0,2“);//限制前面第1到2条数据 if($res&&mysql_num_rows($res)){ while($sql=mysql_fetch_assoc($res)){ $arr[]=$sql; } echo json_encode($arr,JSON_UNESCAPED_UNICODE);//把数据(数组嵌套json类型)转换为字符串输出,这个ajax拿数据经常用 }else{ echo "找不到该数据</br>"; } } selecttable_data("测试猫");//查询name为测试猫
Step 4: According to the modification conditions, the student in the studentinfo database Modify the specified data in the table (change)
Idea: Connect to the server->Connect to the database->Modify the specified data in the data table according to the conditions
function updatetabel_data($name,$chinese){ mysql_query("update student set chinese='$chinese' where name='$name'");//修改student表里为$name的chinese数据修改为$chinese $res=mysql_affected_rows();//返回影响行 if($res>0){ echo "修改成功</br>"; }else{ echo "修改失败</br>"; } } updatetabel_data("测试虎",90);//把测试虎的语文成绩修改为90分
Test Tiger Chinese score has changed from 98 Modify to 90
Step 5: Delete the specified data in the student table of the studentinfo database according to the deletion conditions (delete)
Idea: Connection Server—>Connect to the database—>Delete the specified data in the data table based on conditions
function deletetable_data($name){ mysql_query("delete from student where name='$name'");//删除student表里为$name的整条数据 $res=mysql_affected_rows();//返回影响行 if($res>0){ echo "删除成功</br>"; }else{ echo "删除失败</br>"; } } deletetable_data('测试虎');//删除name为测试虎这条数据

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 is a very popular programming language, and CodeIgniter4 is a commonly used PHP framework. When developing web applications, using frameworks is very helpful. It can speed up the development process, improve code quality, and reduce maintenance costs. This article will introduce how to use the CodeIgniter4 framework. Installing the CodeIgniter4 framework The CodeIgniter4 framework can be downloaded from the official website (https://codeigniter.com/). Down

Pagoda Panel is a powerful panel software that can help us quickly deploy, manage and monitor servers, especially small businesses or individual users who often need to build websites, database management and server maintenance. Among these tasks, MySQL database management is an important job in many cases. So how to use the Pagoda panel for MySQL management? Next, we will introduce it step by step. Step 1: Install Pagoda Panel. Before starting to use Pagoda Panel for MySQL management, you first need to install Pagoda Panel.

How to use PHP to perform database operations in a Linux environment. In modern web applications, the database is an essential component. PHP is a popular server-side scripting language that can interact with various databases. This article will introduce how to use PHP scripts for database operations in a Linux environment and provide some specific code examples. Step 1: Install the Necessary Software and Dependencies Before starting, we need to ensure that PHP and related dependencies are installed in the Linux environment. usually

How to use thinkorm to improve database operation efficiency With the rapid development of the Internet, more and more applications require a large number of database operations. In this process, the efficiency of database operations becomes particularly important. In order to improve the efficiency of database operations, we can use thinkorm, a powerful ORM framework, to perform database operations. This article will introduce how to use thinkorm to improve the efficiency of database operations and illustrate it through code examples. 1. What is thinkormthi?

How to use the FULLOUTERJOIN function in MySQL to obtain the union of two tables. In MySQL, the FULLOUTERJOIN function is a powerful join operation that combines inner joins and outer joins. It can be used to get the union of two tables, that is, combine all the data in the two tables into a single result set. This article will introduce the usage of the FULLOUTERJOIN function and provide some sample code to help readers better understand. FULLOUTERJOIN function

Using PDO for database operations: A better way with PHP In web development, it is very common to use databases for data storage, management, and query. As a language widely used in Web development, PHP naturally provides a wealth of database operation methods. In PHP, you can use MySQLi, PDO and other extension libraries to perform database operations. Among them, PDO is a very commonly used database operation method and has more advantages than other methods. This article will introduce what PDO is to

How to use DoctrineORM in Symfony framework for database operations Introduction: Symfony framework is a popular PHP framework that provides many powerful tools and components for building web applications quickly and easily. One of the key components is DoctrineORM, which provides an elegant way to handle database operations. This article will introduce in detail how to use DoctrineORM to perform database operations in the Symfony framework. we will

When developing a website, application or system, database operations and data interaction are essential. As a commonly used backend development language, PHP's database operation and data interaction capabilities are also very powerful. This article will introduce some commonly used database operation functions and data interaction practices in PHP. At the same time, we will explain these operations and practices with code examples to facilitate readers to better understand and apply them. 1. Database connection Before performing database operations, you first need to connect to the database. In PHP we can use