Implement data insertion and update operations using PHP and SQLite

王林
Release: 2023-07-29 12:16:01
Original
1555 people have browsed it

Use PHP and SQLite to implement data insertion and update operations

SQLite is a lightweight embedded database engine that is widely used in mobile applications and small projects. It is easy to use, efficient, stable and cross-platform, making it the first choice for PHP developers. In this article, we will learn how to perform data insertion and update operations using PHP and SQLite.

First, we need to make sure that the PHP and SQLite extensions are installed. Next, we create a SQLite database file database.db and create a data table named "users" in it. The "users" table will contain the "id", "name" and "email" fields.

<?php
// 连接到SQLite数据库
$db = new SQLite3('database.db');

// 创建users表
$db->exec('CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, email TEXT)');

// 插入数据
$name = 'John Doe';
$email = 'john@example.com';

$db->exec("INSERT INTO users (name, email) VALUES ('$name', '$email')");

// 更新数据
$id = 1;
$newName = 'Jane Smith';

$db->exec("UPDATE users SET name = '$newName' WHERE id = '$id'");

// 关闭数据库连接
$db->close();
?>
Copy after login

In the above code, we first create a SQLite3 object and connect it to the database file. Next, we use the exec() method to execute a SQL statement to create a table named "users" (if the table does not exist). Then, we inserted a piece of data, specifying the values ​​​​of "name" and "email". Finally, we use the UPDATE statement to update the value of the "name" field of the record with id 1 to "Jane Smith".

After executing the above code, we can use the select statement of SQLite3 to check whether the data in the table has been updated successfully.

<?php
// 连接到SQLite数据库
$db = new SQLite3('database.db');

// 查询数据
$result = $db->query('SELECT * FROM users');

while ($row = $result->fetchArray()) {
    echo $row['id'] . ' ' . $row['name'] . ' ' . $row['email'] . '<br>';
}

// 关闭数据库连接
$db->close();
?>
Copy after login

In the above code, we connect to the database again and use the query() method to execute a SELECT statement to obtain data from the "users" table. Then, we used the fetchArray() method to iterate through the query results and output the values ​​of the "id", "name" and "email" fields of each record to the page.

Through the above examples, we learned how to use PHP and SQLite to implement data insertion and update operations. Using SQLite as the database engine can help us quickly build and manage data in small projects. With these basic knowledge, you can use it flexibly in actual development.

The above is the detailed content of Implement data insertion and update operations using PHP and SQLite. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!