Post: [PHP][PDO] Secure Queries
08-28-2015, 01:49 PM #1
Scouse Power
Knowledge is power
(adsbygoogle = window.adsbygoogle || []).push({});
PDO Class
Secure MySQL Queries


::Introduction::
The world we live in is an evil one, so it's important to ensure your web applications are as secure as possible as you don't want any unwanted activity lurking through your systems and/or the data on them. Anyway if you've ever used the mysql you'll probably understand that when executing a query the parameters must be filtered (you can use a variety of methods, such as mysql_real_escape_string). However, most of the time, we just use prepared queries with improved extensions such as PDO and MySQLi -> which is the purpose of this thread.

::Prepared Statements::
The following examples are using PDO.
    
//$database - would be your PDO connection.
$query = $database->prepare("INSERT INTO users (name, password) VALUES Upside Down Happyname, :password)");
$query->bindParam(':name', "StackOverflow");
$query->bindParam(':password', "This is my ngu password.");
$query->execute();

The example above, will only execute the query. It doesn't return any response. You'd have to do yet another line of code to obtain the response.
    $query->fetchAll();


::The Class::
All is good, but this can be really messy if you're executing like 30 queries on one page. So I have constructed a class which will allow you to execute those 5 lines in one. It will also handle the connection for you.
    class Database {
private $Connect, $Db;

public function __construct($host, $user, $pass, $data) {
if(!$this->Connect){
global $config;
try {
$this->Awesome faceb = new PDO("mysql:host={$host};dbname={$data}", $user, $pass, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
$this->Connect = true;
}catch(PDOException $e) {
die($e->getMessage());
}
}
}

public function lastInsertId() {
return $this->Awesome faceb->lastInsertId();
}

public function Query($q, $r = "", $d = null) {
try {
$data = $this->Awesome faceb->prepare($q);
$data->execute($d);

$dbErr = $data->errorInfo();
if ( $dbErr[0] != '00000' ) {
print_r($dbErr->errorInfo());
die();
}

switch(strtolower($r)) {
case "fetchall":
return $data->fetchAll();
case "fetch":
return $data->fetch(PDO::FETCH_ASSOC);
default:
return null;
}

} catch(PDOException $e) {
die($e->getMessage());
}
}
}


::How to use::
Like always with a non-static class, you need to declare the class. When calling this class it takes three arguments; host, user, pass and data which are the details for your sql server and database of your choice. You only need to connect one, no need to connect every time you are executing a query.
    $database = new Database("127.0.0.1", "root", "examplepass", "exampledb");

To execute a query, you would use the "query" function in the class. This function takes three arguments;
[arg1] $q = This is your query, replace all instances with "?" (you can see below)
[arg2] $r = This is your response, currently there are; "", "fetchall" and "fetch". By default it will return nothing.
[arg3] $d = This is an array of your data.
    $database->Query("INSERT INTO users (name, password) VALUES (?, ?)", "", array("StackOverflow", "This is my ngu password."));


Of course, message me if you need any help.
Last edited by Scouse Power ; 08-30-2015 at 04:13 AM.

The following 2 users say thank you to Scouse Power for this useful post:

Chen Madhala, Trefad
08-28-2015, 05:55 PM #2
Chen Madhala
Pokemon Trainer
Originally posted by StackOverflow View Post
PDO Class
Secure MySQL Queries


::Introduction::
The world we live in is an evil one, so it's important to ensure your web applications are as secure as possible as you don't want any unwanted activity lurking through your systems and/or the data on them. Anyway if you've ever used the mysql you'll probably understand that when executing a query the parameters must be filtered (you can use a variety of methods, such as mysql_real_escape_string). However, most of the time, we just use prepared queries with improved extensions such as PDO and MySQLi -> which is the purpose of this thread.

::Prepared Statements::
The following examples are using PDO.
    
//$database - would be your PDO connection.
$query = $database->prepare("INSERT INTO users (name, password) VALUES Upside Down Happyname, :password)");
$query->bindParam(':name', "StackOverflow");
$query->bindParam(':password', "This is my ngu password.");
$query->execute();

The example above, will only execute the query. It doesn't return any response. You'd have to do yet another line of code to obtain the response.
    $query->fetchAll();


::The Class::
All is good, but this can be really messy if you're executing like 30 queries on one page. So I have constructed a class which will allow you to execute those 5 lines in one. It will also handle the connection for you.
    class Database {
private $Connect, $Db;

public function __construct($host, $user, $pass, $data) {
if(!$this->Connect){
global $config;
try {
$this->Awesome faceb = new PDO("mysql:host={$host};dbname={$data}", $user, $pass, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
$this->Connect = true;
}catch(PDOException $e) {
die($e->getMessage());
}
}
}

public function lastInsertId() {
return $this->Awesome faceb->lastInsertId();
}

public function Query($q, $r = "", $d = null) {
try {
$data = $this->Awesome faceb->prepare($q);
$data->execute($d);

$dbErr = $data->errorInfo();
if ( $dbErr[0] != '00000' ) {
print_r($dbErr->errorInfo());
die();
}

switch(strtolower($r)) {
case "fetchall":
return $data->fetchAll();
case "fetch":
return $data->fetch(PDO::FETCH_ASSOC);
default:
return null;
}

} catch(PDOException $e) {
die($e->getMessage());
}
}
}


::How to use::
Like always with a non-static class, you need to declare the class. When calling this class it takes three arguments; host, user, pass and data which are the details for your sql server and database of your choice. You only need to connect one, no need to connect every time you are executing a query.
    $database = new Database("127.0.0.1", "root", "examplepass", "exampledb");

To execute a query, you would use the "query" function in the class. This function takes three arguments;
[arg1] $q = This is your query, replace all instances with "?" (you can see below)
[arg2] $r = This is your response, currently there are; "", "fetchall" and "fetch". By default it will return nothing.
[arg3] $d = This is an array of your data.
    $database->Query("INSERT INTO users (name, password) VALUES (?, ?)", "", array("StackOverflow", "This is my ngu password."));


Of course, message me if you need any help.


Very nice man :yes:

The following user thanked Chen Madhala for this useful post:

Scouse Power
08-31-2015, 11:41 PM #3
Trefad
I defeated!
Dumb question but what is php used for? Websites?
09-01-2015, 12:00 AM #4
Scouse Power
Knowledge is power
Originally posted by Trefad View Post
Dumb question but what is php used for? Websites?


Yeah PHP is used for websites:]

The following user thanked Scouse Power for this useful post:

Trefad
09-01-2015, 12:11 AM #5
Trefad
I defeated!
Ok thanks! Smile

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo