Skip to content

Instantly share code, notes, and snippets.

@kodi
Created February 13, 2011 12:35
Show Gist options
  • Save kodi/824650 to your computer and use it in GitHub Desktop.
Save kodi/824650 to your computer and use it in GitHub Desktop.
Webarto's Database Class
<?php
/**
* @author Webarto
* @copyright 2010
*/
class Database{
public $q = 0;
public $debug = "";
function connect($server, $database, $username, $password, $charset = "UTF8"){
$link = mysql_connect($server, $database, $password);
if(!$link){
die("Unable to connect to database server.");
}
mysql_selectdb($database);
if(function_exists("mysql_set_charset")){
mysql_set_charset($charset, $link);
}else{
mysql_query("SET NAMES $charset");
}
}
function insert($into, $columns, $values){
foreach($columns as $column){
$c .= "$column,";
}
$c = trim($c, ",");
foreach($values as $value){
$v .= "'$value',";
}
$v = trim($v, ",");
$query = ("INSERT INTO $into($c) VALUES($v)");
mysql_query($query);
}
function post($into, $columns, $post){
$column = explode(",", $columns);
foreach($column as $c){
$c = $post[$c];
$v .= "'$c',";
}
$v = trim($v, ",");
$query = ("INSERT INTO $into($columns) VALUES($v)");
mysql_query($query);
}
function update($which, $what, $with, $where){
mysql_query("UPDATE $which SET $what = '$with' WHERE $where");
mysql_query($query);
}
function delete($which, $where){
mysql_query("DELETE FROM $which WHERE $where");
mysql_query($query);
}
function query($query, $assoc = true){
$sql = mysql_query($query);
return $sql;
}
function row($select = "*", $from, $where, $assoc = true){
$query = ("SELECT $select FROM $from WHERE $where LIMIT 1");
$sql = @mysql_query($query);
return $this->fetch($sql, $assoc);
}
function fetch($result, $assoc = true){
if($assoc == true){
return mysql_fetch_assoc($result);
}else{
return mysql_fetch_array($result);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment