Jump to content

Recommended Posts

after researching, i have attempted to nock together a mysql class

 

<?php

/**
* @author Rohan Peters
* @copyright 2008
  */


//Loading what we need
require ('sys_config.php');
require ('sys_error.php');

//Loading master function
function database($db_host, $db_user, $db_pass, $db_db){
$this->server=$db_host;
$this->user=$db_user;
$this->pass=$db_pass;
$this->database=$db_db;
}

//Connecting function
function connect() {
$this->link_id=@mysql_connect($this->server,$this->user,$this->pass);

if (!$this->link_id) {//open failed
	$this->oops("Could not connect to server: <b>$this->server</b>.");
	}
if(!@mysql_select_db($this->database, $this->link_id)) {//no database
	$this->oops("Could not open database: <b>$this->database</b>.");
	}
//Unloading for security
$this->server='';
$this->user='';
$this->pass='';
$this->database='';
}

//Disconnecting function
function close() {
if(!mysql_close()){
	$this->oops("Connection close failed.");
}
}

//Query Function
function query($sql) {

$this->query_id = @mysql_query($sql, $this->link_id);

if (!$this->query_id) {
	$this->oops("<b>MySQL Query fail:</b> $sql");
}

$this->affected_rows = @mysql_affected_rows();

return $this->query_id;
}

//Fetching Function - Single
function fetch_array($query_id=-1) {

if ($query_id!=-1) {
	$this->query_id=$query_id;
}

if (isset($this->query_id)) {
	$this->record = @mysql_fetch_assoc($this->query_id);
}else{
	$this->oops("Invalid query_id: <b>$this->query_id</b>. Records could not be fetched.");
}

if($this->record){
	$this->record=array_map("stripslashes", $this->record);

}
return $this->record;
}

//Fetching Function - All
function fetch_all_array($sql) {
$query_id = $this->query($sql);
$out = array();

while ($row = $this->fetch_array($query_id, $sql)){
	$out[] = $row;
}

$this->free_result($query_id);
return $out;
}

//Freeing results up
function free_result($query_id=-1) {
if ($query_id!=-1) {
	$this->query_id=$query_id;
}
if(!@mysql_free_result($this->query_id)) {
	$this->oops("Result ID: <b>$this->query_id</b> could not be freed.");
}
}

//Single row shot and free
function query_first($query_string) {
$query_id = $this->query($query_string);
$out = $this->fetch_array($query_id);
$this->free_result($query_id);
return $out;
}

//Update function
function query_update($table, $data, $where='1') {
$q="UPDATE `".$this->pre.$table."` SET ";

foreach($data as $key=>$val) {
	if(strtolower($val)=='null') $q.= "`$key` = NULL, ";
	elseif(strtolower($val)=='now()') $q.= "`$key` = NOW(), ";
	else $q.= "`$key`='".$this->escape($val)."', ";
}

$q = rtrim($q, ', ') . ' WHERE '.$where.';';

return $this->query($q);
}
//Insert Function
function query_insert($table, $data) {
$q="INSERT INTO `".$this->pre.$table."` ";
$v=''; $n='';

foreach($data as $key=>$val) {
	$n.="`$key`, ";
	if(strtolower($val)=='null') $v.="NULL, ";
	elseif(strtolower($val)=='now()') $v.="NOW(), ";
	else $v.= "'".$this->escape($val)."', ";
}

$q .= "(". rtrim($n, ', ') .") VALUES (". rtrim($v, ', ') .");";

if($this->query($q)){
	//$this->free_result();
	return mysql_insert_id();
}
else return false;

}


?>

 

it just displays the code

Link to comment
https://forums.phpfreaks.com/topic/135993-solved-oop-mysql-class-attempt/
Share on other sites

As premiso pointed out, you're forgetting the whole wrap your stuff in a class thing...

 


class something { 
   // declare properties here, example...
   var oops;
   var server;
   var user;
   // etc...

function blah() {
   // do stuff here
}

} // end class something

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.