php Editor Banana has launched an article called "PHP PDO Project Example: Hands-on Learning", which provides PHP beginners with a practical project example to help them learn PDO database operations through hands-on practice. The article introduces the project implementation process and code examples in detail, allowing readers to have an in-depth understanding and mastery of the application of PDO in actual projects. Through this sample project, readers can better understand the usage methods and techniques of PDO and improve their PHP programming abilities.
PDO (PHP Data Object) is an object-oriented, database-independent extension in php, which provides different Database A unified interface for interaction with systems (such as Mysql, postgresql, sqlite, etc.). Using PDO can simplify database connections, queries, and update operations while improving code portability and security.
Project Example
In order to experience the power of PDO firsthand, we will create a simple PHP PDO project, which will implement the following functions:
Step 1: Install MySQL and PHP PDO
Before you begin, make sure your system has MySQL Server and the PHP PDO extension installed.
Step 2: Create database and tables
Use the following SQL statement to create a database and table named "people":
CREATE DATABASE people; USE people; CREATE TABLE persons ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255) NOT NULL, age INT NOT NULL );
Step 3: Connect to the database
In the PHP file, use the PDO::connect() function to connect to the MySQL database:
$host = "localhost"; $dbname = "people"; $username = "root"; $passWord = ""; try { $conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTioN); } catch (PDOException $e) { die("Error connecting to database: " . $e->getMessage()); }
Step 4: Insert record
Use PDO::prepare() and PDO::execute() functions to insert a record:
$name = "John Doe"; $age = 30; $stmt = $conn->prepare("INSERT INTO persons (name, age) VALUES (?, ?)"); $stmt->execute([$name, $age]);
Step 5: Update the record
Use the PDO::prepare() and PDO::execute() functions to update a record:
$id = 1; $newAge = 32; $stmt = $conn->prepare("UPDATE persons SET age = ? WHERE id = ?"); $stmt->execute([$newAge, $id]);
Step 6: Delete the record
Use the PDO::prepare() and PDO::execute() functions to delete a record:
$id = 2; $stmt = $conn->prepare("DELETE FROM persons WHERE id = ?"); $stmt->execute([$id]);
Step 7: Query records
Use the PDO::query() function to query all records:
$stmt = $conn->query("SELECT * FROM persons"); $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
Step 8: Display results
Use a loop to display query results:
foreach ($results as $record) { echo "ID: " . $record["id"] . " | Name: " . $record["name"] . " | Age: " . $record["age"] . "<br>"; }
in conclusion
Through this PHP PDO project example, you have learned how to use PDO to interact with a MySQL database. PDO provides a unified interface to easily connect, query, and update data from different database systems. Applying PDO in actual projects will significantly improve your PHP development efficiency and code portability.
The above is the detailed content of PHP PDO project example: hands-on learning. For more information, please follow other related articles on the PHP Chinese website!