distancefinder/db-functions.php
2016-05-24 00:51:25 +02:00

72 lines
1.8 KiB
PHP

<?php
function dbConnect(){
mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or exit(mysql_error());
mysql_select_db(DB_NAME) or exit(mysql_error());
}
function dbLogError($query=''){
file_put_contents(DIRPATH.DB_ERRORS_FILE, @date('Y-m-d H:i:s ').mysql_error()." $query \n" , FILE_APPEND);
}
function dbQuery($query){
$result = mysql_query($query) or dbLogError($query);
return $result;
}
function dbEscape($string){
$string = mysql_real_escape_string($string);
return $string;
}
function dbSelect($query){
$rows = array();
$result = mysql_query($query) or dbLogError($query);
if(mysql_num_rows($result)){
while($row = mysql_fetch_object($result)){
$rows[] = $row;
}
}
return $rows;
}
function dbGetRow($query){
$result = mysql_query($query) or dbLogError($query);
if(mysql_num_rows($result)){
return mysql_fetch_object($result);
}else{
return null;
}
}
function dbGetInsertId(){
return mysql_insert_id();
}
function dbGetVal($query){
$result = mysql_query($query) or dbLogError($query);
if(mysql_num_rows($result)){
return mysql_result($result, 0, 0);
}
return '';
}
function dbInsert($table, $fields){
foreach($fields as $name => $value){
$fields[$name] = mysql_real_escape_string($value);
}
$query = sprintf("INSERT INTO `%s` (`%s`) VALUES ('%s')", $table, implode(array_keys($fields), '`,`'), implode($fields, "','"));
mysql_query($query) or dbLogError($query);
}
function dbUpdate($table, $fields, $where=''){
foreach($fields as $name => $value) {
$value = mysql_real_escape_string($value);
$fields[$name] = "`$name`='$value'";
}
$query = sprintf("UPDATE `%s` SET %s %s", $table, implode($fields, ','), $where);
mysql_query($query) or dbLogError($query);
}