Skip to content

Instantly share code, notes, and snippets.

@jlebrech
Created December 1, 2010 14:19
Show Gist options
  • Save jlebrech/723536 to your computer and use it in GitHub Desktop.
Save jlebrech/723536 to your computer and use it in GitHub Desktop.
<?php
// common
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);
}
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
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,",","=");
}
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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment