Home >Backend Development >PHP Problem >How to modify and save data using PHP? (code example)
PHP is a scripting language widely used in server-side programming. It is flexible, highly scalable, and easy to learn, making it one of the mainstream languages for web development. In PHP, modifying and saving data is one of the common operations. This article will introduce how to use PHP to write, modify and save data.
Before using PHP to operate the database, you need to connect to the database first. First we need to use PHP's built-in function mysqli_connect() to connect to the MySQL database. The following is a sample code to connect to a MySQL database:
$servername = "localhost"; $username = "username"; $password = "password"; $dbname = "mydb"; // 创建连接 $conn = mysqli_connect($servername, $username, $password, $dbname); // 检测连接 if (!$conn) { die("Connection failed: " . mysqli_connect_error()); }
In the above code, $servername represents the name of the database server, $username represents the database username, $password represents the database password, and $dbname represents the connection to database name. If the connection is successful, the $conn variable will remain valid.
After connecting to the database, we can use PHP to execute the query statement and obtain the results. The following is a sample query code:
$sql = "SELECT id, name, email FROM users"; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { // 输出每行数据 while($row = mysqli_fetch_assoc($result)) { echo "id: " . $row["id"]. " - Name: " . $row["name"]. " Email: " . $row["email"]. "<br>"; } } else { echo "0 results"; }
In the above code, the $sql variable stores a query statement that will return all records in the users table. After executing the query, the query results are stored in the $result variable. If the query returns results, we can use the mysqli_fetch_assoc() function to get the data for each row.
If we want to modify the data in the database, we need to write an UPDATE statement and send it using the mysqli_query() function to the database. The following is a sample code that modifies records in the users table:
$sql = "UPDATE users SET name='John Smith' WHERE id=1"; if (mysqli_query($conn, $sql)) { echo "Record updated successfully"; } else { echo "Error updating record: " . mysqli_error($conn); }
In the above code, the $sql variable stores an UPDATE statement, which changes the user name of user ID 1 to "John Smith ". If the modification is successful, the program will output "Record updated successfully", otherwise it will output an error message.
If we want to add new records to the database, we need to write an INSERT statement and use the mysqli_query() function to It is sent to the database. The following is a sample code to add a new user to the users table:
$sql = "INSERT INTO users (name, email) VALUES ('John Doe', 'johndoe@example.com')"; if (mysqli_query($conn, $sql)) { echo "New record created successfully"; } else { echo "Error: " . $sql . "<br>" . mysqli_error($conn); }
In the above code, the $sql variable stores an INSERT statement, which adds a new user to the users table. If the addition is successful, the program will output "New record created successfully", otherwise it will output an error message.
After completing the operation on the database, we need to close the database connection to release resources. The connection can be closed using PHP's built-in function mysqli_close(). The following is a sample code to close the database connection:
mysqli_close($conn);
In the above code, the $conn variable was previously created using the mysqli_connect() function.
Summary
The above is the process code for modifying and saving data in PHP. First, we need to connect to the database, then perform query and modification operations, and save the data if necessary. Once done, we need to close the database connection to release resources. Mastering these basic operations will allow us to easily handle data modification and saving operations in PHP applications.
The above is the detailed content of How to modify and save data using PHP? (code example). For more information, please follow other related articles on the PHP Chinese website!