Created
December 1, 2010 16:11
-
-
Save jlebrech/723706 to your computer and use it in GitHub Desktop.
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 | |
// common | |
/* | |
#usage | |
push_implode($an_array, ",", "="); | |
*/ | |
function push_implode($set, $glue, $op){ | |
$set_array = array(); | |
foreach($set as $key => $value){ | |
array_push( | |
$set_array, | |
sprintf("%s %s %s", | |
mysql_real_escape_string($key), | |
$op, | |
stripslashes(mysql_real_escape_string($value)) | |
) | |
); | |
} | |
return implode($glue,$set_array); | |
} | |
/* | |
#usage | |
time_ago(array("hours"=>5)); | |
*/ | |
function time_ago($ago){ | |
return mktime($ago["hours"]-date("H"), date("i")-$ago["mins"], date("s")-$ago["seconds"], date("m")-$ago["months"], date("d")-$ago["days"],date("Y"),$ago["years"]); | |
} | |
// mysql db | |
/* | |
#usage | |
db_connect('host','user','pass','db'); | |
*/ | |
function db_connect($host,$user,$pass,$db){ | |
mysql_connect($host, $user, $pass); | |
mysql_select_db($db); | |
} | |
function and_where($where){ | |
return push_implode($where," and "," "); | |
} | |
function value_set($set){ | |
return push_implode($set,",","="); | |
} | |
// run an sql query and process the resultset into an array | |
function sql_query($query){ | |
$result = mysql_query($query); | |
$result_array = array(); | |
while($row = mysql_fetch_assoc($result)){ | |
array_push($result_array,$row); | |
} | |
return $result_array; | |
} | |
function sql_select($from,$where){ | |
$query = sprintf("SELECT * FROM %s WHERE %s ;", | |
mysql_real_escape_string($from), | |
and_where($where) | |
); | |
echo $query; | |
return sql_query($query); | |
} | |
/* | |
#usage | |
sql_update("table_name",array(array('field_to_update'=>"'value'")),array(array("where_field ="=>"wherevalue"))); | |
*/ | |
function sql_update($what, $set, $where){ | |
$query = sprintf("UPDATE %s SET %s WHERE %s;", mysql_real_escape_string($what), value_set($set), and_where($where)); | |
echo $query; | |
return sql_query($query); | |
} | |
/* | |
#usage | |
sql_insert("users", | |
array("id"=>"'$id'", | |
"username"=>"'$username'", | |
"password"=>"'$password'")); | |
*/ | |
function sql_insert($where,$what){ | |
$field_array = array(); | |
$value_array = array(); | |
foreach($what as $key => $value){ | |
array_push($field_array,"`$key`"); | |
array_push($value_array,$value); | |
} | |
$query = sprintf("INSERT INTO %s (%s) values (%s);", mysql_real_escape_string($where), implode(",",$field_array), implode(",",$value_array)); | |
echo $query; | |
return mysql_query($query); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment