Home - IT - PHP PDO Cheatsheet

PHP PDO Cheatsheet

PHP with PDO CheatSheet

Here is my cheatsheet for PDO.

table: users

[alert-note]
CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `first_name` varchar(255) DEFAULT NULL,
  `username` varchar(255) DEFAULT NULL,
  `email` varchar(255) DEFAULT NULL,
  `active` tinyint(1) DEFAULT '0',
  `password` text,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
[/alert-note]
[alert-note]
<?php


$con = new PDO('mysql:host=127.0.0.1;dbname=pdo_db','root','');


$drivers = PDO::getAvailableDrivers();

var_dump($drivers);
[/alert-note]

 

[alert-note]
<?php 


try {

$con = new PDO('mysql:host=127.0.0.1;dbname=pdo_dbs','root','');



} catch (PDOException $e) {

die("For some reason the database connection failed");


}
[/alert-note]

 

[alert-note]


		
[alert-note]


		
[alert-note]


		
[alert-note]


		
[alert-note]
<?php


$con = new PDO('mysql:host=127.0.0.1;dbname=pdo_db','root','');

$first_name = 'John';

$password = abc;



$user = $con->prepare("
SELECT * FROM users
WHERE first_name = :first_name
AND password = :password

");


$user->bindValue(':first_name', $first_name);
$user->bindValue(':password', $password);


$user->execute();


$user = $user->fetchAll(PDO::FETCH_OBJ);

var_dump($user);
[/alert-note] [/alert-note]
[alert-note]
<?php $con = new PDO('mysql:host=127.0.0.1;dbname=pdo_db','root',''); $first_name = $_GET['first_name']; $user = $con->prepare("
	SELECT * FROM users
	WHERE first_name LIKE :first_name


	");


$user->bindValue(':first_name', "$first_name%");



$user->execute();


$user = $user->fetchAll(PDO::FETCH_OBJ);

var_dump($user);
[/alert-note]
[alert-note]
<?php


$con = new PDO('mysql:host=127.0.0.1;dbname=pdo_db','root','');

$first_name = $_GET['first_name'];



$user = $con->prepare("
SELECT * FROM users
WHERE first_name = :first_name


");


$user->bindParam(':first_name', $first_name);


$first_name = "John";

$user->execute();


$user = $user->fetchAll(PDO::FETCH_OBJ);

var_dump($user);
[/alert-note]
[alert-note]
<?php $con = new PDO('mysql:host=127.0.0.1;dbname=pdo_db','root',''); $first_name = $con->quote($_GET['first_name']);

die($first_name);

$user = $con->query("
	SELECT * FROM users
	WHERE first_name = $first_name


	");

var_dump($user->fetch(PDO::FETCH_OBJ));

[/alert-note]
[alert-note]

[alert-note]
$con = new PDO('mysql:host=127.0.0.1;dbname=pdo_db','root','');

$id = 2;

$user = $con->prepare("
	INSERT INTO users(first_name, username, email, active, password)
	VALUES (:first, :usern, :email, :active, :pass )

	");

$userExcution = $user->execute([

	'first'  => 'Edwin',
	'usern'  => 'rico',
	'email'  => 'edwin@codingfaculty.com',
	'active' =>  1,
	'pass'   =>  'youcannotknowmypassword',

	]);


[/alert-note]
[alert-note]


		
[alert-note]

[alert-note]
$con = new PDO('mysql:host=127.0.0.1;dbname=pdo_db','root','');

$update = $con->query("
UPDATE users SET first_name = 'George'
WHERE id = 4

");


$delete = $con->query("
DELETE FROM users

");



echo $update->rowCount;
[/alert-note]

 

I hope this useful and it comes handy.

Leave a Reply

Your email address will not be published. Required fields are marked *

*