PHP Classes

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

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-01-28 (8 months ago) RSS 2.0 feedNot enough user ratingsTotal: 140 All time: 9,211 This week: 38Up
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
Age: 33
All time rank: 2434144 in India India
Week rank: 48 Up7 in India India Up
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 (8)  
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

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:140
This week:0
All time:9,211
This week:38Up