irwan

PHP Database connectivity

Nov 16th, 2011
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.55 KB | None | 0 0
  1. //Database Class with quick connectivity and useful database functio
  2.  
  3.  
  4.  
  5. //ClsDatabase_DB
  6. // $db=new ClsDatabase_db();$db->ClsDatabase_conn();
  7.         class ClsDatabase__db{
  8.             function ClsDatabase_conn($dbname="##database_name##"){
  9.              mysql_connect ("localhost", "##username##", "##password##") or die ('I cannot connect to the database because: ' . mysql_error());
  10.                         mysql_select_db ($dbname);
  11.             }
  12.             function ClsDatabase_query($query){
  13.                 $query = @mysql_query($query) or die(mysql_error());
  14.                 return $query;
  15.             }
  16.             function ClsDatabase_rs($query) {
  17.                 $query = @mysql_fetch_array($query);
  18.                 return $query;
  19.             }
  20.                 function auto_id($tbl,$field,$strCondition="") {
  21.                 //GET THE MAXIMUM auto id of a table
  22.                         $strCondition=$strCondition=="" ? "" : "where $strCondition";
  23.                         $sql="Select MAX($field) as maxseq from $tbl $strCondition";
  24.                         $rowsMAX = mysql_fetch_array(mysql_query($sql));
  25.               return $rowsMAX['maxseq'] + 1;
  26.                 }
  27.                 function insert($table,$arrFieldValues,$debug=0)
  28.                 {
  29.                 /*
  30.                 INSERT record into database $table
  31.                 Example:
  32.                 $arrFieldValues=array("id" => 1,                                                        // leaving blank if this an auto increment field
  33.                                                          "user_fname"   => "John",   //(or $_POST["user_fname"])
  34.                                                          "user_lname"   =>  "Doe");
  35.                 insert("users",$arrFieldValues);
  36.                 If debug is set to 1, sql statement will be display
  37.                 */          
  38.                 $arrFields=array_keys($arrFieldValues);
  39.                 $arrValues=array_values($arrFieldValues);
  40.                 $escVals=array();
  41.                 foreach ($arrValues as $val)
  42.                 {
  43.                         if(!is_numeric($val)) $val="'".mysql_escape_string($val)."'";
  44.                         $escVals[]=$val;
  45.                 }
  46.                 $sql="INSERT INTO $table (".join(', ',$arrFields).") VALUES(".join(', ',$escVals).")";
  47.                 if($debug) echo $sql;
  48.                 mysql_query($sql);
  49.                 return mysql_insert_id();
  50.                 }
  51.        
  52.                 function update($table,$arrFieldValues,$arrConditions,$debug=0)
  53.                 {      
  54.                 /*
  55.                 UPDATE record into database $table
  56.                 Example:
  57.                 $arrFieldValues=array("id" => 1,                                                        // leaving blank if this an auto increment field
  58.                                                          "user_fname"   => "John",   //(or $_POST["user_fname"])
  59.                                                          "user_lname"   =>  "Doe");
  60.                 $arrConditions=array("user_lname" => "Doe");
  61.                 update("users",$arrFieldValues,$arrConditions);
  62.                 */          
  63.                         $arrUpdates=array();
  64.                         foreach ($arrFieldValues as $field => $val)
  65.                         {
  66.                                 if(!is_numeric($val)) $val="'".mysql_escape_string($val)."'";
  67.                                 $arrUpdates[]= "$field = $val";
  68.                         }
  69.                         $arrWhere=array();
  70.                         foreach ($arrConditions as $field => $val){
  71.                                 if(!is_numeric($val)) $val="'".mysql_escape_string($val)."'";
  72.                                 $arrWhere[]= "$field = $val";
  73.                         }
  74.                         $sql="UPDATE $table SET ".join(', ',$arrUpdates)." WHERE ".join(' AND ',$arrWhere);
  75.                         if($debug) echo $sql;
  76.                         return mysql_query($sql);
  77.                 }
  78.                
  79.                 function delete($table,$arrConditions)
  80.                 {
  81.                        
  82.                         $arrWhere=array();
  83.                         foreach ($arrConditions as $field => $val)
  84.                         {
  85.                                 if(!is_numeric($val)) $val="'".mysql_escape_string($val)."'";
  86.                                 $arrWhere[]= "$field = $val";
  87.                         }
  88.                          $sql="DELETE FROM $table WHERE ".join(' AND ',$arrWhere);
  89.                         $hRes=mysql_query($sql);
  90.                 return mysql_affected_rows($hRes);
  91.                 }
  92.                
  93.                
  94.                 function boolDuplicate($table,$arrConditions)
  95.                 {
  96.                         //CHECK for duplicate record with the same conditions
  97.                         $arrWhere=array();
  98.                         foreach ($arrConditions as $field => $val)
  99.                         {
  100.                                 if(!is_numeric($val)) $val="'".mysql_escape_string($val)."'";
  101.                                 $arrWhere[]= "$field = $val";
  102.                         }
  103.                         $sql="SELECT * FROM $table WHERE ".join(' AND ',$arrWhere);
  104.                         $hRes=mysql_query($sql);
  105.                         if(mysql_num_rows(mysql_query($sql))>0)
  106.                                 return 1;
  107.                         else
  108.                                 return 0;
  109.                 }
  110.                 function getOne($tbl,$field,$strCondition="",$debug=0)
  111.                 {
  112.                 // GET data for a single record
  113.                 // EXAMPLE: getOne("users","user_fname","id=1")
  114.                         $strCondition=$strCondition=="" ? "" : "where $strCondition";
  115.                         $sql="Select $field as fieldReturn from $tbl $strCondition";
  116.                         if($debug) echo $sql;
  117.                         $rowsReturn = mysql_fetch_array(mysql_query($sql));
  118.               return $rowsReturn["fieldReturn"];
  119.                 }
  120.             function getAllRows($sql,$debug=0)
  121.                  {
  122.                         //GET ALL RECORDS based on the $sql statement, return an array
  123.                                if($debug) echo $sql;
  124.                                $result=mysql_query($sql);
  125.                                 while($rows = mysql_fetch_array($result))
  126.                                         $rowsReturn[]=$rows;
  127.                                 return $rowsReturn;
  128.                  }
  129.                
  130.                  function getRow($sql,$debug=0)
  131.                  {
  132.                         //GET 1 Record base on the $sql statment
  133.                                if($debug) echo $sql;
  134.                                 return mysql_fetch_array(mysql_query($sql));
  135.                  }
  136.                 function getArray($tbl,$field,$strCondition="",$delimeter="#",$debug=0)
  137.                 {
  138.                         //GET a concatenate string with the delimeter
  139.                         // EXAMPLE: getArray("users","user_fname","",",") => John,Mary
  140.                         $strCondition=$strCondition=="" ? "" : "where $strCondition";
  141.                         $sql="Select $field as fieldReturn from $tbl $strCondition ORDER BY $field";
  142.                         if($debug) echo $sql;
  143.                         $result=mysql_query($sql);
  144.                         while($rowsReturn = mysql_fetch_array($result)) $lstReturn.=$rowsReturn["fieldReturn"].$delimeter;
  145.               return "#".$lstReturn;
  146.                 }
  147.                 function ClsDatabase_die(){ mysql_close(); }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment