Skip to content

Instantly share code, notes, and snippets.

@bradtraversy
Created February 21, 2018 12:56
Show Gist options
  • Star 65 You must be signed in to star a gist
  • Fork 31 You must be signed in to fork a gist
  • Save bradtraversy/a77931605ba9b7cf3326644e75530464 to your computer and use it in GitHub Desktop.
Save bradtraversy/a77931605ba9b7cf3326644e75530464 to your computer and use it in GitHub Desktop.
PDO Class
<?php
/*
* PDO DATABASE CLASS
* Connects Database Using PDO
* Creates Prepeared Statements
* Binds params to values
* Returns rows and results
*/
class Database {
private $host = DB_HOST;
private $user = DB_USER;
private $pass = DB_PASS;
private $dbname = DB_NAME;
private $dbh;
private $error;
private $stmt;
public function __construct() {
// Set DSN
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
$options = array (
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
// Create a new PDO instanace
try {
$this->dbh = new PDO ($dsn, $this->user, $this->pass, $options);
} // Catch any errors
catch ( PDOException $e ) {
$this->error = $e->getMessage();
}
}
// Prepare statement with query
public function query($query) {
$this->stmt = $this->dbh->prepare($query);
}
// Bind values
public function bind($param, $value, $type = null) {
if (is_null ($type)) {
switch (true) {
case is_int ($value) :
$type = PDO::PARAM_INT;
break;
case is_bool ($value) :
$type = PDO::PARAM_BOOL;
break;
case is_null ($value) :
$type = PDO::PARAM_NULL;
break;
default :
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}
// Execute the prepared statement
public function execute(){
return $this->stmt->execute();
}
// Get result set as array of objects
public function resultset(){
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_OBJ);
}
// Get single record as object
public function single(){
$this->execute();
return $this->stmt->fetch(PDO::FETCH_OBJ);
}
// Get record row count
public function rowCount(){
return $this->stmt->rowCount();
}
// Returns the last inserted ID
public function lastInsertId(){
return $this->dbh->lastInsertId();
}
}
@zed-dz
Copy link

zed-dz commented Aug 4, 2018

Thank you

@kevin81767
Copy link

Nice

@alcoach
Copy link

alcoach commented Sep 24, 2018

Thanks for this

@Hassan-Boulhilt
Copy link

Thanks and Good Job

@Mutepuka
Copy link

awesome thanks for sharing...

@ThabangMohale
Copy link

awesome

@Mahmoudalziem
Copy link

fantastic

@dejvid98
Copy link

dejvid98 commented May 2, 2020

Thanks Papa Brad!

@ernesthenry
Copy link

Thank you Brad

@absaaraslam
Copy link

god I need help

@Jnalis
Copy link

Jnalis commented Feb 13, 2022

where actual you need help @absaaraslam

@absaaraslam
Copy link

@Jnalis I'm having issues with the 3D Secure Card Payments. It gives an error and does not authenticate it.

@Jnalis
Copy link

Jnalis commented Feb 14, 2022

can you share the type of error you are getting so people here we can help

@ismaelrak
Copy link

Thanks i appreciate this so much

@Guyverix
Copy link

Even after 6 years, this is still helping all of us. Thank you for posting this for us n00bs :)

@absaaraslam
Copy link

Came a long way from here and very proud<3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment