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 !
-
class Common {
-
-
function json2query($table_name, $primary_key=null, $output) {
-
$output = str_replace('\\','',$output);
-
$counter = 0;
-
$array = json_decode($output, true);
-
foreach ($array as $key=>$value) {
-
$query = array();
-
foreach ($value as $field_name=>$field_value) {
-
if ($primary_key==$field_name) {
-
$where_clause = $field_name.'='.$field_value;
-
} else {
-
array_push($query,'`'.$field_name.'`='.SQLFiendly($field_value));
-
}
-
}
-
-
$array_query[$counter++] = sprintf('UPDATE %s SET %s WHERE %s',$table_name, implode(",",$query), $where_clause);
-
-
}
-
return $array_query;
-
}
-
/**
-
* Enter description here…
-
*
-
* @param unknown_type $search
-
* @param unknown_type $replace
-
* @param unknown_type $subject
-
* @return unknown
-
*/
-
function StrReplaceFirst($search, $replace, $subject) {
-
// replaces the first occurance of $search in $subject with $replace;
-
// returns $subject unchanged if no match
-
// may be rewritten with regexpressions if there are volunteers
-
if ((!$subject) or (!$search)) return $subject;
-
$sp = strpos($subject, $search);
-
if ($sp!==false) {
-
return (substr($subject, 0, $sp) . $replace . substr($subject, $sp+strlen($search)));
-
} else {
-
return $subject;
-
}
-
-
}
-
-
/**
-
* Enter description here…
-
*
-
* @param unknown_type $theValue
-
* @param unknown_type $theType
-
* @param unknown_type $theDefinedValue
-
* @param unknown_type $theNotDefinedValue
-
* @return unknown
-
*/
-
function sqlValue($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") {
-
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;
-
switch ($theType) {
-
case "text":
-
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
-
break;
-
case "int":
-
$theValue = ($theValue !== "") ? intval($theValue) : "NULL";
-
break;
-
}
-
-
return $theValue;
-
}
-
-
function dbconnect() {
-
global $config;
-
$link = mysql_pconnect($config['host'], $config['username'], $config['password'])
-
or exit(mysql_error());
-
mysql_select_db($config['database'], $link);
-
return true;
-
}
-
-
/**
-
* Enter description here…
-
*
-
* @param unknown_type $result
-
* @param unknown_type $total
-
* @return unknown
-
*/
-
function mysqlresult2array($result, $total = true) {
-
$array=array();
-
$total = mysql_num_rows($result);
-
//$array['total'] = $total;
-
while ($row=mysql_fetch_assoc($result)) {
-
//var_dump($row);
-
$array[]=$row;
-
}
-
-
mysql_free_result($result);
-
return '{"data":'.json_encode($array).'}';
-
}
-
-
/**
-
* Enter description here…
-
*
-
* @param unknown_type $query
-
* @param unknown_type $values
-
* @return unknown
-
*/
-
function query($query, $values=NULL) {
-
$this->dbconnect();
-
if (is_array($values)) {
-
foreach ($values as $value=>$type) {
-
$query = $this->StrReplaceFirst('%s', $this->sqlValue($value, $type), $query);
-
}
-
}
-
-
$execute = mysql_query($query) or print mysql_error()."\n
-
<h1>".$query."</h1>
-
";
-
-
if (preg_match('/SELECT/', $query)) {
-
return $this->mysqlresult2array($execute);
-
} else {
-
return true;
-
}
-
-
}
-
-
/**
-
* Enter description here…
-
*
-
* @param unknown_type $url
-
*/
-
function goto($url=null) {
-
if (!empty($url)) {
-
header('Location: '.$url);
-
} else {
-
-
}
-
}
-
}
And a really simple usage:
-
$a = new Common();
-
$json_output = $a->query('SELECT * FROM table WHERE 1');
-
echo $json_output;


Write a Comment