Saturday, 27 June 2015

A few MySQL functions for PHP


MySQL: Connect
/*--------------------------------------------------------
* Function: DBconnect
* Skapar en anslutning till databas och tabellen vi arbetar mot
*
* Parameters:
* database - Används inte
*
* Returns:
* Pekare till databasen eller en textsträng med felmeddelande
*
* Usage:
* $sqlid = DBconnect("");
*------------------------------------------------------ */
function DBconnect($database) {
 
# Anslut till mySQL
$sqlid=@mysql_connect(DB_DATABASE, DB_USER, DB_PASSWORD);
if ($sqlid=="") {
print("<em>Det gick inte att få kontakt med databasen '".DB_DATABASE."'.</em>");
exit;
}
 
# Välj tabell att hantera
$tabell=@mysql_select_db(DB_TABLE);
if ($tabell=="") {
print("<em>Tabellen '".DB_TABLE."' finns inte i databasen.</em>");
exit;
}
return $sqlid;
}
MySQL: Escape characters
# ---------------------------------------------------------
# Escapes characters to be MySQL ready
# ---------------------------------------------------------
function escape($string) {
$sqlid = DBconnect("");
if(get_magic_quotes_gpc()) $string = stripslashes($string);
return mysql_real_escape_string($string);
}
MySQL: Insert
/*--------------------------------------------------------
* Function: sql_insert($table, $data)
* Creates an SQL query for inserting data into a table *
*
* Parameters:
* $table -
* $data -
*
* Returns:
* Escaped SQL query ready to de executed
*
* Usage:
* $table = DB_TABLE;
* $data['created']=$datum;
* $sql = sql_insert($table, $data);
* $result = mysql_query($sql);
* if (!$result) { print(mysql_error()); }
*------------------------------------------------------ */
function sql_insert($table, $data) {
$q = "INSERT INTO `".$table."` (";
 
# Skapa variabler för fältnamn och värden
foreach($data as $key=>$val) {
if(strtolower($val)=='null') {
$f.= "`$key`, ";
$v.= "NULL, ";
} elseif(strtolower($val)=='now()') {
$f.= "`$key`, ";
$v.= "NOW(), ";
} else {
$f.= "`$key`, ";
$v.= "'".escape($val)."', ";
}
}
 
# Ta bort sista ", "
$f = rtrim($f, ', '); $v = rtrim($v, ', ');
 
# Skapa hela SQL-satsen
$q = $q.$f.") VALUES (".$v.")";
 
return $q;
}
MySQL: Update
/*--------------------------------------------------------
* Function: sql_update($table, $data, $where='1')
* Creates an SQL query for updating data in a table
*
* Parameters:
* $table -
* $data -
* $where -
*
* Returns:
* Escaped SQL query ready to de executed
*
* Usage:
* $table = DB_TABLE;
* $data['uppdaterad']=$datum;
* $where = "id=$myid";
* $sql = sql_update($table, $data, $where);
* $result = mysql_query($sql);
* if (!$result) { print(mysql_error()); }
*------------------------------------------------------ */
function sql_update($table, $data, $where='1') {
$q="UPDATE `".$table."` SET ";
 
foreach($data as $key=>$val) {
if(strtolower($val)=='null') $q.= "`$key`=NULL, ";
elseif(strtolower($val)=='now()') $q.= "`$key`=NOW(), ";
elseif (is_numeric($value)) $q.= "`$key`=".intval($value).", ";
else $q.= "`$key`='".escape($val)."', ";
}
 
# Ta bort sista ", "
$q = rtrim($q, ', ') . ' WHERE '.$where.';';
 
return $q;
}

0 comments:

Post a Comment