php basic pdo mysql connection and CRUD example
In this article I will be explaining php basic PDO mysql connection and queries example
PDO stands for PHP Data Objects, it provides Multipledatabase access. by multiple database access it means that if we change our database from MySql to MS Access or Sqlite, we just need to make small change in database connection line, all other coding will work perfectly for any kind of database with same structure of database tables.
every thing has its advantages and disadvantages, some major advantages of PDO are
- Multiple RDBMS
- Parameters binding which looks clean and prevents sql injection
- Its faster then mysql or mysqli
few disadvantages are
- Do not have enough features as mysqli has like multiple queries
- Only OOP implementation, no procedural
we should use PDO if having plan to change RDBMS in futues, we can use PDO for
- PHP to MS Sql
- PHP to MS Access
- PHP to Oracle
- PHP to Sybase
- PHP to Sqlite
- PHP to Mysql
- PHP to any database with out changing queries or syntax
PDO Datbase Connection
we can connect to any database like MS SQL SQLite MS Access MySql etc using pdo
few examples are given below pick yours from it.
MS SQL connection
1 2 3 4 5 6 7 8 9 10 11 12 |
//database credentials for MS SQL database try{ $host = 'localhost'; $db_name = 'my_db'; $user_name = 'alam'; $password = '2Str0ngPW$'; $db = new PDO("mssql:host=$host;dbname=$db_name, $user_name, $password"); } catch(PDOException $e) { echo $e->getMessage(); } |
SQLite connection
1 2 3 4 5 6 7 8 9 10 11 |
//database credentials for SQLite database try{ $db_path = 'databases/subfolder/'; $db_file = 'my_sqlite_db.db'; $db_full_path = $db_path.$db_file; $db = new PDO("sqlite:".$db_full_path); } catch(PDOException $e) { echo $e->getMessage(); } |
MS Access connection
1 2 3 4 5 6 7 |
//MS Access database connection try{ $db = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=D:\files\my_db.mdb;Uid=Admin"); } catch(PDOException $e) { echo $e->getMessage(); } |
MySQL connection
1 2 3 4 5 6 7 8 9 10 11 12 |
//database credentials for MySQL database try{ $host = 'localhost'; $db_name = 'my_db'; $user_name = 'root'; $password = ''; $db = new PDO("mysql:host=$host;dbname=$db_name", $user_name, $password); } catch(PDOException $e) { echo $e->getMessage(); } |
these were different RDBMS connections you can google and find more. we have wrapped all connections in try catch to handle errors.
PDO Select Query
use the following code snippet to select data from database
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
//selection try{ $statement = $db->prepare("select * from users where name = :name"); $statement->execute(array(':name' => "alam")); while($row = $statement->fetch()){ echo $row['name'].': '.$row['email'].'<br>'; }//end while //close connection $db->null; } catch(PDOException $e) { echo $e->getMessage(); } //or if you want the whole result as array // $rows = $statement->fetchAll() //then foreach rows array and display it |
PDO Update Query
we can update record(s) using the following code snippet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
//update query try{ $qry = "UPDATE `users` SET `name` = :name, `email` = :email, `mobile` = :mobile, WHERE `id` = :id"; $statement = $db->prepare($qry); $statement->bindValue(":name", $_GET['name']); $statement->bindValue(":email", $_GET['email']); $statement->bindValue(":mobile", $_GET['mobile']); $statement->bindValue(":id", $_GET['id']); $count = $statement->execute(); echo $count.' records updated'; //close connection $db->null; } catch(PDOException $e) { echo $e->getMessage(); } |
PDO Insert Query
we can Insert record using with PDO as the following code snippet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
//insert query try{ $qry = "INSERT INTO MyGuests (name, email, mobile) VALUES (:name, :email, :mobile)"; $statement = $db->prepare($qry); $statement->bindValue(":name", $_GET['name']); $statement->bindValue(":email", $_GET['email']); $statement->bindValue(":mobile", $_GET['mobile']); $statement->execute(); //you can insert another record just by replacing values as below //no need to write query and prepare again $statement->bindValue(":name", '2'.$_GET['name']); $statement->bindValue(":email", '2'.$_GET['email']); $statement->bindValue(":mobile", '2'.$_GET['mobile']); $statement->execute(); //close connection $db->null; } catch(PDOException $e) { echo $e->getMessage(); } |
PDO DeleteQuery
we can Delete record(s) using with PDO as the following code snippet
1 2 3 4 5 6 7 8 9 10 11 12 13 |
//insert query try{ $qry = "DELETE FROM users WHERE id= :id"; $statement = $db->prepare($qry); $statement->bindValue(":id", $_GET['id']); $statement->execute(); //close connection $db->null; } catch(PDOException $e) { echo $e->getMessage(); } |
these are all crud operations with PDO, I did it in the simplest way so that beginners can understand easily, customize it as you need, comment if you have any issue. and keep in touch to have these kind of articles like our facebook and google plus pages.
Comments
Thanks a lot Brother. very nicely explained.