PHP Classes

PHP PDO CRUD Example: Example code to implement CRUD operations with PDO

Recommend this page to a friend!
  Info   View files Example   View files View files (8)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (1)    
Last Updated Ratings Unique User Downloads Download Rankings
2024-01-28 (5 days ago) RSS 2.0 feedNot yet rated by the usersTotal: 89 This week: 89All time: 9,911 This week: 1Up
Version License PHP version Categories
tutorial-php-pdo-cru 1.0.0The PHP License5PHP 5, Databases, Design Patterns, Gl...
Description 

Author

This package provides examples of code to implement CRUD operations with PDO.

It provides a script that creates a PDO object to connect to a MySQL database using parameters retrieved from a configuration script.

Other scripts can use the PDO object to perform CRUD operations like:

- Create a new table record

- Retrieve all records of a table

- Retrieve a table record that matches a condition

- Update a table record that matches a condition with new field values

- Delete a table record that matches a condition

Picture of Channaveer Hakari
  Performance   Level  
Name: Channaveer Hakari <contact>
Classes: 8 packages by
Country: India India
Innovation award
Innovation award
Nominee: 2x

Example

<?php
require_once './db.php';
try {
   
$userQuery = $pdo->prepare("
        INSERT INTO `users`
            (`first_name`, `last_name`, `email`, `password`)
        VALUES
            (:first_name, :last_name, :email, :password)
    "
);
   
$user = $userQuery->execute([
       
':first_name' => 'Channaveer',
       
':last_name' => 'Hakari',
       
':email' => 'someemail@gmail.com',
       
/** For the sake of demonstration I am using MD5, using hash for more secure */
       
':password' => md5('password')
    ]);

    if (!
$user) {
        throw new
Exception('Error in adding user details');
    }

   
$lastInsertedIs = $pdo->lastInsertId();
} catch (
Exception $e) {
   
/** Handle all your errors here */
   
exit($e->getMessage());
}


Details

PHP PDO CRUD Operations Demo

This is a plain PHP PDO CRUD demo. All the CRUD operations reside in (create.php, read*.php, update.php & delete.php files)

For more details on the same use this link https://stackcoder.in/posts/why-and-how-to-use-php-pdo-with-crud-examples

To read more articles on PHP / LARAVEL use this link https://stackcoder.in


  Files folder image Files  
File Role Description
Accessible without login Plain text file create.php Example Example script
Accessible without login Plain text file db.php Aux. PDO database object creation script
Accessible without login Plain text file delete.php Example Example script
Accessible without login Plain text file init.php Conf. Configuration script
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Plain text file read_all.php Example Example script
Accessible without login Plain text file read_by_condition.php Example Example script
Accessible without login Plain text file update.php Example Example script

 Version Control Unique User Downloads Download Rankings  
 100%
Total:89
This week:89
All time:9,911
This week:1Up