Angel’s Blog

Someone right now is looking pretty tired, staring at a laptop trying to get inspired…

My Common class for JSON & php


I always wanted to share some source, so i will do it now. It’s a very basic Common class in php for connecting to database and executing queries. Nothing special but may help to somebody new to php.
This Class provides a easy way to get a JSON formated output from your mysql queries and update the records back to the database.
Works only with php 5.2 or higher !

  1. class Common {
  2.  
  3.  function json2query($table_name, $primary_key=null, $output) {
  4.   $output = str_replace('\\','',$output);
  5.   $counter = 0;
  6.   $array = json_decode($output, true);
  7.   foreach ($array as $key=>$value) {
  8.    $query = array();
  9.    foreach ($value as $field_name=>$field_value) {
  10.     if ($primary_key==$field_name) {
  11.      $where_clause = $field_name.'='.$field_value;
  12.     } else {
  13.      array_push($query,'`'.$field_name.'`='.SQLFiendly($field_value));
  14.     }
  15.    }
  16.  
  17.    $array_query[$counter++] = sprintf('UPDATE %s SET %s WHERE %s',$table_name, implode(",",$query), $where_clause);
  18.  
  19.   }
  20.   return $array_query;
  21.  }
  22.  /**
  23.   * Enter description here…
  24.   *
  25.   * @param unknown_type $search
  26.   * @param unknown_type $replace
  27.   * @param unknown_type $subject
  28.   * @return unknown
  29.   */
  30.  function StrReplaceFirst($search, $replace, $subject) {
  31.   // replaces the first occurance of $search in $subject with $replace;
  32.   // returns $subject unchanged if no match
  33.   // may be rewritten with regexpressions if there are volunteers
  34.   if ((!$subject) or (!$search)) return $subject;
  35.   $sp = strpos($subject, $search);
  36.   if ($sp!==false) {
  37.    return (substr($subject, 0, $sp) . $replace . substr($subject, $sp+strlen($search)));
  38.   } else {
  39.    return $subject;
  40.   }
  41.  
  42.  }
  43.  
  44.  /**
  45.   * Enter description here…
  46.   *
  47.   * @param unknown_type $theValue
  48.   * @param unknown_type $theType
  49.   * @param unknown_type $theDefinedValue
  50.   * @param unknown_type $theNotDefinedValue
  51.   * @return unknown
  52.   */
  53.  function sqlValue($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") {
  54.   $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;
  55.   switch ($theType) {
  56.   case "text":
  57.    $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  58.   break;
  59.   case "int":
  60.    $theValue = ($theValue !== "") ? intval($theValue) : "NULL";
  61.   break;
  62.   }
  63.  
  64.  return $theValue;
  65.  }
  66.  
  67.  function dbconnect() {
  68.   global $config;
  69.   $link = mysql_pconnect($config['host'], $config['username'], $config['password'])
  70.    or exit(mysql_error());
  71.   mysql_select_db($config['database'], $link);
  72.   return true;
  73.  }
  74.  
  75.  /**
  76.   * Enter description here…
  77.   *
  78.   * @param unknown_type $result
  79.   * @param unknown_type $total
  80.   * @return unknown
  81.   */
  82.  function mysqlresult2array($result, $total = true) {
  83.   $array=array();
  84.   $total = mysql_num_rows($result);
  85.   //$array['total'] = $total;
  86.   while ($row=mysql_fetch_assoc($result)) {
  87.    //var_dump($row);
  88.    $array[]=$row;
  89.   }
  90.  
  91.    mysql_free_result($result);
  92.    return '{"data":'.json_encode($array).'}';
  93.  }
  94.  
  95.  /**
  96.   * Enter description here…
  97.   *
  98.   * @param unknown_type $query
  99.   * @param unknown_type $values
  100.   * @return unknown
  101.   */
  102.  function query($query, $values=NULL) {
  103.   $this->dbconnect();
  104.   if (is_array($values)) {
  105.    foreach ($values as $value=>$type) {
  106.     $query = $this->StrReplaceFirst('%s', $this->sqlValue($value, $type), $query);
  107.    }
  108.   }
  109.  
  110.   $execute = mysql_query($query) or print mysql_error()."\n
  111. <h1>".$query."</h1>
  112. ";
  113.  
  114.   if (preg_match('/SELECT/', $query)) {
  115.    return $this-&gt;mysqlresult2array($execute);
  116.   } else {
  117.    return true;
  118.   }
  119.  
  120.  }
  121.  
  122.  /**
  123.   * Enter description here…
  124.   *
  125.   * @param unknown_type $url
  126.   */
  127.  function goto($url=null) {
  128.   if (!empty($url)) {
  129.    header('Location: '.$url);
  130.   } else {
  131.  
  132.   }
  133.  }
  134. }

And a really simple usage:

  1. $a = new Common();
  2. $json_output = $a-&gt;query('SELECT * FROM table WHERE 1');
  3. echo $json_output;






Write a Comment

Note: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>