Home > Article > Backend Development > PHP add, delete, modify and check result array
In PHP programming, addition, deletion, modification and query are very basic operations, which are used in almost every project. This article will focus on these four operations and return a result array. The purpose of the result array is to directly obtain information about the success or failure of the operation during operation, so that the program design can be more efficient.
In PHP, the add operation uses the INSERT INTO statement. The syntax is as follows:
INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);
Among them, table_name is where data is to be inserted. The table name, column1, column2, etc. are the column names in the table where data is to be inserted, and then the corresponding data is added through the VALUES keyword.
After inserting data, you can get the number of inserted rows through the mysqli_affected_rows() function. Based on the value of the row number, you can determine whether the insertion was successful.
The following is a sample code:
//连接到数据库 $conn = mysqli_connect($servername, $username, $password, $dbname); //设置字符集 mysqli_set_charset($conn, "utf8"); //SQL语句 $sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe', 'john@example.com')"; //执行SQL语句 if (mysqli_query($conn, $sql)) { $result = array( 'status' => true, 'message' => '插入成功', 'row' => mysqli_affected_rows($conn) ); } else { $result = array( 'status' => false, 'message' => '插入失败: ' . mysqli_error($conn), 'row' => mysqli_affected_rows($conn) ); } //关闭数据库连接 mysqli_close($conn); //返回结果数组 return $result;
In the code, when mysqli_query() is executed successfully, we will return a result array, indicating whether the addition operation is successful, and the row of inserted data number.
The deletion operation uses the DELETE FROM statement, the syntax is as follows:
DELETE FROM table_name WHERE some_column = some_value
Among them, table_name is the name of the table to delete data, WHERE Statements are conditions used to filter data.
After completing the deletion of data, you can also obtain the number of deleted rows through the mysqli_affected_rows() function. Based on the value of the number of rows, you can determine whether the deletion was successful.
A sample code is given below:
//连接到数据库 $conn = mysqli_connect($servername, $username, $password, $dbname); //设置字符集 mysqli_set_charset($conn, "utf8"); //SQL语句 $sql = "DELETE FROM MyGuests WHERE id=3"; //执行SQL语句 if (mysqli_query($conn, $sql)) { $result = array( 'status' => true, 'message' => '删除成功', 'row' => mysqli_affected_rows($conn) ); } else { $result = array( 'status' => false, 'message' => '删除失败: ' . mysqli_error($conn), 'row' => mysqli_affected_rows($conn) ); } //关闭数据库连接 mysqli_close($conn); //返回结果数组 return $result;
In the code, when mysqli_query() is executed successfully, we will return a result array indicating whether the deletion operation is successful and the row of deleted data number.
The modification operation uses the UPDATE statement, the syntax is as follows:
UPDATE table_name SET column1=value1, column2=value2, ... WHERE some_column=some_value
Among them, table_name is the name of the table to modify the data, and the SET statement Used to set new values, and the WHERE statement is used to filter data conditions.
After modifying the data, you can also obtain the number of modified rows through the mysqli_affected_rows() function. Based on the value of the row number, you can determine whether the modification was successful.
The following is a sample code:
//连接到数据库 $conn = mysqli_connect($servername, $username, $password, $dbname); //设置字符集 mysqli_set_charset($conn, "utf8"); //SQL语句 $sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2"; //执行SQL语句 if (mysqli_query($conn, $sql)) { $result = array( 'status' => true, 'message' => '修改成功', 'row' => mysqli_affected_rows($conn) ); } else { $result = array( 'status' => false, 'message' => '修改失败: ' . mysqli_error($conn), 'row' => mysqli_affected_rows($conn) ); } //关闭数据库连接 mysqli_close($conn); //返回结果数组 return $result;
In the code, when mysqli_query() is executed successfully, we will return a result array, indicating whether the modification operation is successful, and the row of modified data number.
The query operation uses the SELECT statement. The syntax is as follows:
SELECT column1, column2, ... FROM table_name WHERE some_column=some_value
Among them, column1, column2, etc. are the column names to be queried. The FROM statement specifies the name of the table to be queried, and the WHERE statement is used to filter the data.
After completing the query, you need to use the mysqli_fetch_assoc() function to convert the query results into an array. If the query result is empty, an empty array is returned.
The following is a sample code:
//连接到数据库 $conn = mysqli_connect($servername, $username, $password, $dbname); //设置字符集 mysqli_set_charset($conn, "utf8"); //SQL语句 $sql = "SELECT id, firstname, lastname FROM MyGuests ORDER BY lastname"; //执行SQL语句 $result = mysqli_query($conn, $sql); //将查询结果转化为数组 if (mysqli_num_rows($result) > 0) { $data = array(); while($row = mysqli_fetch_assoc($result)) { $data[] = $row; } $result = array( 'status' => true, 'message' => '查询成功', 'data' => $data ); } else { $result = array( 'status' => true, 'message' => '查询成功,但无数据', 'data' => array() ); } //关闭数据库连接 mysqli_close($conn); //返回结果数组 return $result;
In the code, we first execute the SQL statement and obtain the query result $result. Then, use the mysqli_num_rows() function to determine whether the query result is empty. If not, use the mysqli_fetch_assoc() function to convert the query result into an array, obtain the number of rows of the query data, and return the result array; if the query result is empty, Then the result array is returned directly.
Through the result arrays of the above four operations, we can process data more efficiently and increase PHP programming efficiency.
The above is the detailed content of PHP add, delete, modify and check result array. For more information, please follow other related articles on the PHP Chinese website!