/model/database.php
https://github.com/aaraashkhan/echoback · PHP · 364 lines · 212 code · 77 blank · 75 comment · 26 complexity · 4847ee9f40c047654103a9eedfee1db8 MD5 · raw file
- <?php
- /**
- DataBase Module which is
- only resposible for makeing predined queries
- and it is not a singelton design
- */
- class MySqlPredefined{
- private $user_name;
- private $server;
- private $password;
- private $database;
- public $dbObject;
- private $db_handle;
- private $db_found;
- private $DBH;
-
- /**
- Initializing the database
- and handling it's connection
- */
- public function __construct($server,$user_name,$password,$database) {
- $this->server = $server;
- $this->user_name = $user_name;
- $this->password = $password;
- $this->database = $database;
-
- $this->DBH = new PDO("mysql:host=$server;dbname=$database", $user_name, $password,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
- $this->DBH ->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $this->DBH ->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
- }
-
- /**
- A general method that returns the whole row
- of a defined table
- */
- public function generalSelectWithNoWhere($table_name){
- if ($this->DBH) {
- //echo "SELECT * from $table_name WHERE $clause";die();
- $STH = $this->DBH->query("SELECT * from $table_name");
-
- $STH->setFetchMode(PDO::FETCH_ASSOC);
-
- // $SQL = "SELECT * FROM $table_name WHERE $clause";
- // //echo $SQL;die();
- // $result = mysql_query($SQL);
- // mysql_close($db_handle);
-
- return $STH;
- }
- else {
- print "Database NOT Found ";
-
- }
- }
-
- /**
- A general method that returns the whole row
- of a defined table with predicator
- */
- public function generalSelectWithPredicator($table_name,$clause){
-
-
- if ($this->DBH) {
- //echo "SELECT * from $table_name WHERE $clause";die();
- $STH = $this->DBH->query("SELECT * from $table_name WHERE $clause");
-
- $STH->setFetchMode(PDO::FETCH_ASSOC);
-
- // $SQL = "SELECT * FROM $table_name WHERE $clause";
- //echo $SQL;die();
- // $result = mysql_query($SQL);
- // mysql_close($db_handle);
-
- return $STH;
- }
- else {
- print "Database NOT Found ";
-
- }
- }
-
- /**
- A general method that returns the whole row
- of a defined table with predicator
- */
- public function generalSelectWithPredicatorAndOrder($table_name,$clause,$order){
-
-
- if ($this->DBH) {
- //echo "SELECT * FROM ORDER BY $order $table_name WHERE $clause";die();
- $STH = $this->DBH->query("SELECT * FROM $table_name WHERE $clause ORDER BY $order");
-
- $STH->setFetchMode(PDO::FETCH_ASSOC);
-
- // $SQL = "SELECT * FROM $table_name WHERE $clause ORDER BY $order";
- // echo $SQL;die();
- // $result = mysql_query($SQL);
- // mysql_close($db_handle);
-
- return $STH;
- }
- else {
- print "Database NOT Found ";
-
- }
- }
-
- /**
- A general method that returns the count
- of field
- */
- public function generalCountWithPredicator($table_name,$row,$clause){
- $db_handle = mysql_connect($this->server,$this->user_name,$this->password);
- $db_found = mysql_select_db($this->database, $db_handle);
-
- if ($db_found) {
-
- $SQL = "SELECT COUNT('$row') AS TOTAL FROM $table_name WHERE $clause";
- //echo $SQL;
- $result = mysql_query($SQL);
- mysql_close($db_handle);
-
- return $result;
- }
- else {
-
- print "Database NOT Found ";
- mysql_close($db_handle);
-
- }
- }
-
- public function newGeneralCountWithPredicator($table_name,$row,$clause){
- if ($this->DBH) {
- //echo "SELECT COUNT('$row') AS TOTAL FROM $table_name WHERE $clause";die();
- $STH = $this->DBH->query("SELECT COUNT('$row') AS TOTAL FROM $table_name WHERE $clause");
-
- $STH->setFetchMode(PDO::FETCH_ASSOC);
-
- // $SQL = "SELECT * FROM $table_name WHERE $clause ORDER BY $order";
- // echo $SQL;die();
- // $result = mysql_query($SQL);
- // mysql_close($db_handle);
- $row = $STH -> fetch();
-
- return $row['TOTAL'];
-
- //die();
- }
- else {
- print "Database NOT Found ";
-
- }
-
- }
-
- /**
- A general method that returns the sum
- of field
- */
- public function generalSumWithPredicator($table_name,$row,$clause){
- $db_handle = mysql_connect($this->server,$this->user_name,$this->password);
- $db_found = mysql_select_db($this->database, $db_handle);
-
- if ($db_found) {
-
- $SQL = "SELECT SUM($row) AS TOTAL FROM $table_name WHERE $clause";
- //die($SQL);
- $result = mysql_query($SQL);
- mysql_close($db_handle);
-
- return $result;
- }
- else {
-
- print "Database NOT Found ";
- mysql_close($db_handle);
-
- }
- }
-
- /**
- * A general method that returns the rows
- * seperated for foreach
- */
- public function generalSelectWithPredicatorSeperated($table_name,$clause){
- $db_handle = mysql_connect($this->server,$this->user_name,$this->password);
- $db_found = mysql_select_db($this->database, $db_handle);
-
- if ($db_found) {
-
- $SQL = "SELECT * FROM $table_name WHERE $clause";
-
- $result = mysql_query($SQL);
- mysql_close($db_handle);
- $data = array();
- while($row = mysql_fetch_assoc($result))
- {
- $data[] = $row;
- }
-
- $colNames = array_keys(reset($data));
- return $data;
- }
- else {
- print "Database NOT Found ";
- mysql_close($db_handle);
- }
- }
-
- public function publicGeneralSelectWithPredicatorSeperated($query){
- if ($this->DBH) {
- //echo "SELECT * from $table_name WHERE $clause";die();
- $STH = $this->DBH->query($query);
-
- $STH->setFetchMode(PDO::FETCH_ASSOC);
-
- // $SQL = "SELECT * FROM $table_name WHERE $clause";
- // //echo $SQL;die();
- // $result = mysql_query($SQL);
- // mysql_close($db_handle);
-
- return $STH;
- }
- else {
- print "Database NOT Found ";
-
- }
- }
-
- /**
- * A general method to insert data to a table with data
- */
- public function publicGeneralInsertStatement($query){
-
- if ($this->DBH) {
- $fields = "";
- $values = "";
- $SQL = $query;
- //echo "$SQL";die();
- $qurey = $this->DBH->prepare($SQL);
- $qurey->execute();
- return $this->DBH->lastInsertId();
- }
- else {
- print "Database NOT Found ";
-
- }
- }
-
- public function generalInsertStatement($table_name,$field,$data_array){
-
- if ($this->DBH) {
- $fields = "";
- $values = "";
- $SQL = "INSERT INTO $table_name (";
-
- foreach ($field as &$value) {
- $fields .= " $value ,";
- }
- $fields = substr($fields,0,-1);
-
- foreach ($data_array as &$valuee) {
- if ($valuee != NULL) {
- $values .= " '$valuee' ,";
- }
- else{
- $values .= " NULL ,";
- }
- }
- $values = substr($values,0,-1);
- unset($value);
- unset($valuee);
-
- $SQL .= $fields.") VALUES (".$values.")";
- //echo "$SQL";die();
- $qurey = $this->DBH->prepare($SQL);
- $qurey->execute();
- return $this->DBH->lastInsertId();
- }
- else {
- print "Database NOT Found ";
-
- }
- }
-
- public function generalInsert2($table_name,$fields,$data_array)
- {
- mysql_connect($this->server, $this->user_name, $this->password) or die(mysql_error());
- mysql_select_db($this->database) or die(mysql_error());
- $fields = "";
- $values = "";
- $SQL = "INSERT INTO $table_name (";
-
- foreach ($field as &$value) {
- $fields .= " $value ,";
- }
- $fields = substr($fields,0,-1);
-
- foreach ($data_array as &$valuee) {
- $values .= " '$valuee' ,";
- }
- $values = substr($values,0,-1);
- unset($value);
- unset($valuee);
- echo "$SQL";die();
- $SQL .= $fields.") VALUES (".$values.")";
- mysql_query($SQL);
-
- }
-
- /**
- A general method to update data to a table with data
- */
- public function generalUpdateStatement($SQL){
- // echo $SQL;
- // die();
- $db_handle = mysql_connect($this->server,$this->user_name,$this->password);
- mysql_query ("set character_set_client='utf8'");
- mysql_query ("set character_set_results='utf8'");
-
- mysql_query ("set collation_connection='utf8_general_ci'");
- $db_found = mysql_select_db($this->database, $db_handle);
-
- if ($db_found) {
- $result = mysql_query($SQL);
-
- mysql_close($db_handle);
-
- return $result;
- }
- else {
-
- print "Database NOT Found ";
- mysql_close($db_handle);
-
- }
- }
-
- /**
- A general method to delete a row from table
- */
- public function generalDeleteStatement($table_name,$clause){
- $db_handle = mysql_connect($this->server,$this->user_name,$this->password);
- $db_found = mysql_select_db($this->database, $db_handle);
-
- if ($db_found) {
- //echo "DELETE FROM $table_name WHERE $clause";die();
- $result = mysql_query("DELETE FROM $table_name WHERE $clause");
-
- mysql_close($db_handle);
-
- return $result;
- }
- else {
-
- print "Database NOT Found ";
- mysql_close($db_handle);
-
- }
- }
-
- }
- ?>