Very simple PHP PDO class Class for reducing development time
<?php
class db
{
public $isConnected;
protected $datab;
public function __construct($username, $password, $host, $dbname, $options=array()){
$this->isConnected = true;
try {
$this->datab = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options);
$this->datab->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->datab->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
}
catch(PDOException $e) {
$this->isConnected = false;
throw new Exception($e->getMessage());
}
}
public function Disconnect(){
$this->datab = null;
$this->isConnected = false;
}
public function getRow($query, $params=array()){
try{
$stmt = $this->datab->prepare($query);
$stmt->execute($params);
return $stmt->fetch();
}catch(PDOException $e){
throw new Exception($e->getMessage());
}
}
public function getRows($query, $params=array()){
try{
$stmt = $this->datab->prepare($query);
$stmt->execute($params);
return $stmt->fetchAll();
}catch(PDOException $e){
throw new Exception($e->getMessage());
}
}
public function insertRow($query, $params){
try{
$stmt = $this->datab->prepare($query);
$stmt->execute($params);
}catch(PDOException $e){
throw new Exception($e->getMessage());
}
}
public function updateRow($query, $params){
return $this->insertRow($query, $params);
}
public function deleteRow($query, $params){
return $this->insertRow($query, $params);
}
}
//USAGE
/*
Connecting to DataBase
$database = new db("root", "", "localhost", "database", array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
Getting row
$getrow = $database->getRow("SELECT email, username FROM users WHERE username =?", array("yusaf"));
Getting multiple rows
$getrows = $database->getRows("SELECT id, username FROM users");
inserting a row
$insertrow = $database ->insertRow("INSERT INTO users (username, email) VALUES (?, ?)", array("yusaf", "[email protected]"));
updating existing row
$updaterow = $database->updateRow("UPDATE users SET username = ?, email = ? WHERE id = ?", array("yusafk", "[email protected]", "1"));
delete a row
$deleterow = $database->deleteRow("DELETE FROM users WHERE id = ?", array("1"));
disconnecting from database
$database->Disconnect();
checking if database is connected
if($database->isConnected){
echo "you are connected to the database";
}else{
echo "you are not connected to the database";
}
*/
Very simple PHP PDO class
<?php
class db
{
public $isConnected;
protected $datab;
public function __construct($username, $password, $host, $dbname, $options=array()){
$this->isConnected = true;
try {
$this->datab = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options);
$this->datab->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->datab->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
}
catch(PDOException $e) {
$this->isConnected = false;
throw new Exception($e->getMessage());
}
}
public function Disconnect(){
$this->datab = null;
$this->isConnected = false;
}
public function getRow($query, $params=array()){
try{
$stmt = $this->datab->prepare($query);
$stmt->execute($params);
return $stmt->fetch();
}catch(PDOException $e){
throw new Exception($e->getMessage());
}
}
public function getRows($query, $params=array()){
try{
$stmt = $this->datab->prepare($query);
$stmt->execute($params);
return $stmt->fetchAll();
}catch(PDOException $e){
throw new Exception($e->getMessage());
}
}
public function insertRow($query, $params){
try{
$stmt = $this->datab->prepare($query);
$stmt->execute($params);
}catch(PDOException $e){
throw new Exception($e->getMessage());
}
}
public function updateRow($query, $params){
return $this->insertRow($query, $params);
}
public function deleteRow($query, $params){
return $this->insertRow($query, $params);
}
}
//USAGE
/*
Connecting to DataBase
$database = new db("root", "", "localhost", "database", array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
Getting row
$getrow = $database->getRow("SELECT email, username FROM users WHERE username =?", array("yusaf"));
Getting multiple rows
$getrows = $database->getRows("SELECT id, username FROM users");
inserting a row
$insertrow = $database ->insertRow("INSERT INTO users (username, email) VALUES (?, ?)", array("yusaf", "[email protected]"));
updating existing row
$updaterow = $database->updateRow("UPDATE users SET username = ?, email = ? WHERE id = ?", array("yusafk", "[email protected]", "1"));
delete a row
$deleterow = $database->deleteRow("DELETE FROM users WHERE id = ?", array("1"));
disconnecting from database
$database->Disconnect();
checking if database is connected
if($database->isConnected){
echo "you are connected to the database";
}else{
echo "you are not connected to the database";
}
*/
Class for reducing development time
<?php
class db
{
public $isConnected;
protected $datab;
public function __construct($username, $password, $host, $dbname, $options=array()){
$this->isConnected = true;
try {
$this->datab = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options);
$this->datab->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->datab->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
}
catch(PDOException $e) {
$this->isConnected = false;
throw new Exception($e->getMessage());
}
}
public function Disconnect(){
$this->datab = null;
$this->isConnected = false;
}
public function getRow($query, $params=array()){
try{
$stmt = $this->datab->prepare($query);
$stmt->execute($params);
return $stmt->fetch();
}catch(PDOException $e){
throw new Exception($e->getMessage());
}
}
public function getRows($query, $params=array()){
try{
$stmt = $this->datab->prepare($query);
$stmt->execute($params);
return $stmt->fetchAll();
}catch(PDOException $e){
throw new Exception($e->getMessage());
}
}
public function insertRow($query, $params){
try{
$stmt = $this->datab->prepare($query);
$stmt->execute($params);
}catch(PDOException $e){
throw new Exception($e->getMessage());
}
}
public function updateRow($query, $params){
return $this->insertRow($query, $params);
}
public function deleteRow($query, $params){
return $this->insertRow($query, $params);
}
}
//USAGE
/*
Connecting to DataBase
$database = new db("root", "", "localhost", "database", array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
Getting row
$getrow = $database->getRow("SELECT email, username FROM users WHERE username =?", array("yusaf"));
Getting multiple rows
$getrows = $database->getRows("SELECT id, username FROM users");
inserting a row
$insertrow = $database ->insertRow("INSERT INTO users (username, email) VALUES (?, ?)", array("yusaf", "[email protected]"));
updating existing row
$updaterow = $database->updateRow("UPDATE users SET username = ?, email = ? WHERE id = ?", array("yusafk", "[email protected]", "1"));
delete a row
$deleterow = $database->deleteRow("DELETE FROM users WHERE id = ?", array("1"));
disconnecting from database
$database->Disconnect();
checking if database is connected
if($database->isConnected){
echo "you are connected to the database";
}else{
echo "you are not connected to the database";
}
*/
Quill
- 12k
- 5
- 41
- 93
OP stated he's learning OO, added tag
<?php
class db{
public $isConnected;
protected $datab;
public function __construct($username, $password, $host, $dbname, $options=array()){
$this->isConnected = true;
try {
$this->datab = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options);
$this->datab->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->datab->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
}
catch(PDOException $e) {
$this->isConnected = false;
throw new Exception($e->getMessage());
}
}
public function Disconnect(){
$this->datab = null;
$this->isConnected = false;
}
public function getRow($query, $params=array()){
try{
$stmt = $this->datab->prepare($query);
$stmt->execute($params);
return $stmt->fetch();
}catch(PDOException $e){
throw new Exception($e->getMessage());
}
}
public function getRows($query, $params=array()){
try{
$stmt = $this->datab->prepare($query);
$stmt->execute($params);
return $stmt->fetchAll();
}catch(PDOException $e){
throw new Exception($e->getMessage());
}
}
public function insertRow($query, $params){
try{
$stmt = $this->datab->prepare($query);
$stmt->execute($params);
}catch(PDOException $e){
throw new Exception($e->getMessage());
}
}
public function updateRow($query, $params){
return $this->insertRow($query, $params);
}
public function deleteRow($query, $params){
return $this->insertRow($query, $params);
}
}
//USAGE
/*
Connecting to DataBase
$database = new db("root", "", "localhost", "database", array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
Getting row
$getrow = $database->getRow("SELECT email, username FROM users WHERE username =?", array("yusaf"));
Getting multiple rows
$getrows = $database->getRows("SELECT id, username FROM users");
inserting a row
$insertrow = $database ->insertRow("INSERT INTO users (username, email) VALUES (?, ?)", array("yusaf", "[email protected]"));
updating existing row
$updaterow = $database->updateRow("UPDATE users SET username = ?, email = ? WHERE id = ?", array("yusafk", "[email protected]", "1"));
delete a row
$deleterow = $database->deleteRow("DELETE FROM users WHERE id = ?", array("1"));
disconnecting from database
$database->Disconnect();
checking if database is connected
if($database->isConnected){
echo "you are connected to the database";
}else{
echo "you are not connected to the database";
}
*/
?>
<?php
class db{
public $isConnected;
protected $datab;
public function __construct($username, $password, $host, $dbname, $options=array()){
$this->isConnected = true;
try {
$this->datab = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options);
$this->datab->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->datab->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
}
catch(PDOException $e) {
$this->isConnected = false;
throw new Exception($e->getMessage());
}
}
public function Disconnect(){
$this->datab = null;
$this->isConnected = false;
}
public function getRow($query, $params=array()){
try{
$stmt = $this->datab->prepare($query);
$stmt->execute($params);
return $stmt->fetch();
}catch(PDOException $e){
throw new Exception($e->getMessage());
}
}
public function getRows($query, $params=array()){
try{
$stmt = $this->datab->prepare($query);
$stmt->execute($params);
return $stmt->fetchAll();
}catch(PDOException $e){
throw new Exception($e->getMessage());
}
}
public function insertRow($query, $params){
try{
$stmt = $this->datab->prepare($query);
$stmt->execute($params);
}catch(PDOException $e){
throw new Exception($e->getMessage());
}
}
public function updateRow($query, $params){
return $this->insertRow($query, $params);
}
public function deleteRow($query, $params){
return $this->insertRow($query, $params);
}
}
//USAGE
/*
Connecting to DataBase
$database = new db("root", "", "localhost", "database", array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
Getting row
$getrow = $database->getRow("SELECT email, username FROM users WHERE username =?", array("yusaf"));
Getting multiple rows
$getrows = $database->getRows("SELECT id, username FROM users");
inserting a row
$insertrow = $database ->insertRow("INSERT INTO users (username, email) VALUES (?, ?)", array("yusaf", "[email protected]"));
updating existing row
$updaterow = $database->updateRow("UPDATE users SET username = ?, email = ? WHERE id = ?", array("yusafk", "[email protected]", "1"));
delete a row
$deleterow = $database->deleteRow("DELETE FROM users WHERE id = ?", array("1"));
disconnecting from database
$database->Disconnect();
checking if database is connected
if($database->isConnected){
echo "you are connected to the database";
}else{
echo "you are not connected to the database";
}
*/
?>
<?php
class db{
public $isConnected;
protected $datab;
public function __construct($username, $password, $host, $dbname, $options=array()){
$this->isConnected = true;
try {
$this->datab = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options);
$this->datab->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->datab->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
}
catch(PDOException $e) {
$this->isConnected = false;
throw new Exception($e->getMessage());
}
}
public function Disconnect(){
$this->datab = null;
$this->isConnected = false;
}
public function getRow($query, $params=array()){
try{
$stmt = $this->datab->prepare($query);
$stmt->execute($params);
return $stmt->fetch();
}catch(PDOException $e){
throw new Exception($e->getMessage());
}
}
public function getRows($query, $params=array()){
try{
$stmt = $this->datab->prepare($query);
$stmt->execute($params);
return $stmt->fetchAll();
}catch(PDOException $e){
throw new Exception($e->getMessage());
}
}
public function insertRow($query, $params){
try{
$stmt = $this->datab->prepare($query);
$stmt->execute($params);
}catch(PDOException $e){
throw new Exception($e->getMessage());
}
}
public function updateRow($query, $params){
return $this->insertRow($query, $params);
}
public function deleteRow($query, $params){
return $this->insertRow($query, $params);
}
}
//USAGE
/*
Connecting to DataBase
$database = new db("root", "", "localhost", "database", array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
Getting row
$getrow = $database->getRow("SELECT email, username FROM users WHERE username =?", array("yusaf"));
Getting multiple rows
$getrows = $database->getRows("SELECT id, username FROM users");
inserting a row
$insertrow = $database ->insertRow("INSERT INTO users (username, email) VALUES (?, ?)", array("yusaf", "[email protected]"));
updating existing row
$updaterow = $database->updateRow("UPDATE users SET username = ?, email = ? WHERE id = ?", array("yusafk", "[email protected]", "1"));
delete a row
$deleterow = $database->deleteRow("DELETE FROM users WHERE id = ?", array("1"));
disconnecting from database
$database->Disconnect();
checking if database is connected
if($database->isConnected){
echo "you are connected to the database";
}else{
echo "you are not connected to the database";
}
*/
Loading
default