Skip to content

Instantly share code, notes, and snippets.

@JeremyMorgan
Last active December 14, 2015 17:09
Show Gist options
  • Save JeremyMorgan/5120382 to your computer and use it in GitHub Desktop.
Save JeremyMorgan/5120382 to your computer and use it in GitHub Desktop.
Simple PDO Database Class
<?php
/**
* File: database.class.php
* Super simple PDO class
*
* Author: Jeremy Morgan
*
* This is just a start. No error handling or parameterization yet
*/
class Database {
public $dbh; // database object
private $result; // result set
private function getDbh() {
return $this->dbh;
}
private function setDbh($x) {
$this->dbh = $x;
}
#Constructor
public function __construct() {
#probaby best to store the credentials in a config file
$this->setDbh = new PDO('<SERVERTYPE>:host=<SERVER> ;dbname=<SOMEDB>', '<USERNAME>', '<PASSWORD>');
}
#Execute a query
function query($qry) {
$this->result = $this->dbh->query($qry);
$this->result->execute();
}
#Show result set as an object
function fetchobject() {
$row = $this->result->fetch(PDO::FETCH_OBJ);
return $row;
}
#Show result set as an array
function fetcharray() {
$row = $this->result->fetch(PDO::FETCH_ASSOC);
return $row;
}
#Show result set as a number
function fetchnum() {
$row = $this->result->fetch(PDO::FETCH_NUM);
return $row;
}
#Count total number of rows in database
function checkrowCount() {
$count = count($this->result->fetchAll());
return $count;
}
}
?>
@maxxon15
Copy link

maxxon15 commented May 3, 2013

Line no: 27 setDbh is used as a constant/variable. Just above it is defined as a function.

Is this a bug?

and Line no. 33: Execute is not called as a function.

@JeremyMorgan
Copy link
Author

Thank you, I have made the proper changes to it. I will be updating this Gist soon with a cleaner version soon.

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