Created
October 13, 2017 10:53
-
-
Save phpfiddle/1db06fb614d4993b59263eaa126674f4 to your computer and use it in GitHub Desktop.
[ Posted by Ikechukwu ] abstract sql query commands for use anywhere within the program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
abstract class Model { | |
public function get_object_name() | |
{ | |
return strtolower(get_class($this)); | |
} | |
public function get_props() | |
{ | |
$object_props = get_object_vars($this); | |
return $object_props; | |
} | |
public function create() | |
{ | |
$object_props = $this->get_props(); | |
$columns = "("; | |
$values = "("; | |
foreach($object_props as $key=>$value) | |
{ | |
$columns.=$key.','; | |
$values.=$value.','; | |
} | |
$columns.=")"; | |
$values.=")"; | |
$query_string = "INSERT INTO ".$this->get_object_name()." ".$columns." VALUES ".$values; | |
echo $query_string; | |
} | |
public function delete() | |
{ | |
$object_props = $this->get_props(); | |
$columns = "("; | |
$values = "("; | |
foreach ($object_props as $key => $value) { | |
$columns.=$key.','; | |
$values.=$value.','; | |
} | |
$columns.=")"; | |
$values.=")"; | |
$query_string = "DELETE FROM ".$this->get_object_name()." "." WHERE ".$columns."=". $values; | |
echo $query_string; | |
} | |
public function update() | |
{ | |
$object_props = $this->get_props(); | |
$columns = "("; | |
$values = "("; | |
foreach ($object_props as $key => $value) { | |
$columns.=$key.','; | |
$values.=$value.','; | |
} | |
$columns.=")"; | |
$values.=")"; | |
$query_string = "UPDATE ".$this->get_object_name(). " SET ".$key. "=".$value." WHERE ".$columns." = ".$values; | |
echo $query_string; | |
} | |
public function select(){ | |
$object_props = $this->get_props(); | |
$columns = "("; | |
$values = "("; | |
foreach ($object_props as $key => $value) { | |
$columns.=$key.','; | |
$values.=$value.','; | |
} | |
$columns.=")"; | |
$values.=")"; | |
$query_string = "SELECT ".$columns. "FROM ".$this->get_object_name(); | |
echo $query_string; | |
} | |
public function select_array(){ | |
$object_props = $this->get_props(); | |
$query_string = "SELECT * FROM ".$this->get_object_name()."WHERE "; | |
$items = array($columns =>$values); | |
$array_length = count($items); | |
foreach ($items as $key => $value) { | |
$index = array_search($key,array_keys($items)); | |
$query_string.=$key.'='.$value; | |
if ($index!=$array_length-1) { | |
$query_string.='AND'; | |
} | |
} | |
echo $query_string; | |
} | |
} | |
/*class Auth | |
{ | |
static public function authenticate($credentials = array('' => , );)( | |
) | |
}*/ | |
class Db | |
{ | |
public $servername = 'localhost'; | |
public $username = 'root'; | |
public $password = 'passme@123'; | |
public $dbname = 'testphp'; | |
public function __construct() | |
{ | |
$conn = new mysqli($this->servername, $this->username, $this->password, $this->dbname); | |
if($conn->connect_error) | |
{ | |
die("could not connect". $conn->connect_error); | |
} | |
$this->conn = $conn; | |
return $this; | |
} | |
function query($query) | |
{ | |
$this->conn->query($query); | |
} | |
} | |
/*function createUsers(){ | |
$dbase = new Db; | |
$checkTable = "CREATE TABLE PostTable( | |
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, | |
firstname VARCHAR(30) NOT NULL, | |
messageBody VARCHAR(80) NOT NULL, | |
reg_date TIMESTAMP)"; | |
if ($dbase->query($checkTable) === TRUE) { | |
echo "Table PostTable created successfully"; | |
} else { | |
echo "Error creating table: " . $dbase->error; | |
} | |
if($checkTable !== FALSE){ | |
echo "table already exists, moving on "; | |
}else{ | |
return $checkTable; | |
} | |
$dbase->conn->close(); | |
}*/ | |
class User extends Model { | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment