PHP Classes
elePHPant
Icontem

File: pdo.class.php

Recommend this page to a friend!
  Classes of Name Removed  >  PDO Prepared MySQL Statements Class  >  pdo.class.php  >  Download  
File: pdo.class.php
Role: Class source
Content type: text/plain
Description: Class
Class: PDO Prepared MySQL Statements Class
Prepare and execute MySQL queries using PDO
Author: By
Last change: Updated to version 2.0!

@Removed type and length parameters from both query() and fetch().The class determines the type and the length of the parameters passed.

@Removed connect() and the try-catch block.Use try-catch outside of the class to catch any possible exception.
Date: 2 years ago
Size: 1,217 bytes
 

 

Contents

Class file image Download
<?php
// @ PDO Prepared MySQL Statements Class v2.0
// @ Developed by Takis Maletsas
// @ 2016
class DB
{
    private
$dbh = null;
    public function
__construct($db_host = "localhost", $db_name = "database", $db_user = "root", $db_pswd = "")
    {
       
$this->dbh = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pswd);
       
$this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
    public function
query()
    {
       
$args = func_get_args();
       
$sth = $this->dbh->prepare($args[0]);
        for (
$i = 1; $i < count($args); $i++)
           
$sth->bindParam($i, $args[$i], $this->type($args[$i]), strlen($args[$i]));
        return
$sth->execute();
    }
    public function
fetch()
    {
       
$args = func_get_args();
       
$sth = $this->dbh->prepare($args[0]);
        for (
$i = 1; $i < count($args); $i++)
           
$sth->bindParam($i, $args[$i], $this->type($args[$i]), strlen($args[$i]));
       
$sth->execute();
        return
$sth->fetchAll(PDO::FETCH_ASSOC);
    }
    private function
type($arg)
    {
        return
is_int($arg) ? PDO::PARAM_INT : (is_bool($arg) ? PDO::PARAM_BOOL : PDO::PARAM_STR);
    }
}
?>