Forum Statistics
- Forum Members:
- Total Threads:
- Total Posts: 7
There are 1 users currently browsing forums.
|
 |

11-03-2004
|
 |
Graduate Student
|
|
Join Date: Sep 2004
Posts: 431
Rep Power: 6
|
|
Mysql Library (class to make it easier)
|
|
sup guys heres a class i made thats very basic still.. need to add more but this sets up and works for most needs i only tried reading databases for now just check it out ill add on to it later. You might even luv it:P
PHP Code:
<?php
class MysqlConnection
{
var $_resource;
var $_host;
var $_user;
var $_pass;
var $_db;
var $_queries = array();
function MysqlConnection($address, $user, $pass, $db)
{
$this->_host = $address;
$this->_user = $user;
$this->_pass = $pass;
$this->_db = $db;
$this->connect();
$this->useDB($this->_db);
}
function connect()
{
//Default Connection
//$this->_resource = mysql_connect($this->_host, $this->_user, $this->_pass) or trigger_error(mysql_error(),E_USER_ERROR);
//Persistant Connection
$this->_resource = mysql_pconnect($this->_host, $this->_user, $this->_pass) or trigger_error(mysql_error(),E_USER_ERROR);
}
function disconnect()
{
//Persistant connections cant disconnect
mysql_close($this->_resource);
}
function useDB($db)
{
mysql_select_db($db, $this->_resource);
}
function query($id, $q)
{
$$id = array($q);
$tmp = mysql_query($q, $this->_resource) or die(mysql_error());
array_push($$id, $tmp);
$this->_queries[$id] = $$id;
unset($$id);
}
function get_assoc($id)
{
return mysql_fetch_assoc($this->_queries[$id][1]);
}
}
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
//The fun starts here everthing above was just the class
//the only real code is down below:P
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
// server user password database
$sql = new MysqlConnection('blah', 'blah', 'blah', 'blah');
// database
//$sql->useDB('newdb');
// identifier query
$sql->query('committees', 'SELECT * FROM committee_master');
// identifier
print_r($sql->get_assoc('committees'));
$sql->disconnect();
?>
|

11-04-2004
|
 |
Founder
|
|
Join Date: Aug 2004
Location: Bournemouth, UK
Posts: 2,517
Rep Power: 19
|
|
Re: Mysql Library (class to make it easier)
|
|
That is pretty cool!  I'm guessing it would be most useful if you were using loads of databases. And connecting to MySQL all over the place in your code...good stuff
|

11-04-2004
|
|
Just a "Little" Kid
|
|
Join Date: Oct 2004
Posts: 64
Rep Power: 6
|
|
Re: Mysql Library (class to make it easier)
|
|
dude, cool. if ya wanna to know what possibilities to add, chack out phpbb mysql database class. and you should allow use both functions: persistent and not persistent connections.
|

11-04-2004
|
 |
Founder
|
|
Join Date: Aug 2004
Location: Bournemouth, UK
Posts: 2,517
Rep Power: 19
|
|
Re: Mysql Library (class to make it easier)
|
|
Quote:
|
Cyber_Syrnyk originally posted:
dude, cool. if ya wanna to know what possibilities to add, chack out phpbb mysql database class. and you should allow use both functions: persistent and not persistent connections.
|
heh, i was just having a look at the vbulletin one  Its pretty mad but nice  I dont know if i can acctually post it considering VBulletin is a commercial product
|

11-04-2004
|
 |
Graduate Student
|
|
Join Date: Sep 2004
Posts: 431
Rep Power: 6
|
|
Re: Mysql Library (class to make it easier)
|
|
im going to be adding to the class later today and ill add tons of features etc so check back in a while to look at it~!
|

11-04-2004
|
 |
Graduate Student
|
|
Join Date: Sep 2004
Posts: 431
Rep Power: 6
|
|
Re: Mysql Library (class to make it easier)
|
|
there we go i worked on it and ive added some nifty features, (and i added the option to use persistant connections true or false)
the main feature now in the insert feature which does all the sql for you. You just give it the identifier, columns to insert into, table, and values.
how the whole class works:
heres a small outline
it uses identifiers which are differnet values in a array called _queries where the value actually leads to another array which contains the query, results, resource id, error and like such. The class uses that to pump data in / out and orderly. heres the new class with the insert function---im trying to make a read function which reads a specific row but theres some problems with that and i need to work it out later.
PHP Code:
<?php
class MysqlConnection
{
var $_resource;
var $_host;
var $_user;
var $_pass;
var $_db;
var $_queries = array();
function MysqlConnection($address, $user, $pass, $db, $persistant)
{
$this->_host = $address;
$this->_user = $user;
$this->_pass = $pass;
$this->_db = $db;
$this->connect($persistant);
$this->useDB($this->_db);
}
function connect($persistant)
{
if (!$persistant)
{
//Default Connection
$this->_resource = mysql_connect($this->_host, $this->_user, $this->_pass) or trigger_error(mysql_error(),E_USER_ERROR);
}
else
{
//Persistant Connection
$this->_resource = mysql_pconnect($this->_host, $this->_user, $this->_pass) or trigger_error(mysql_error(),E_USER_ERROR);
}
}
function disconnect()
{
//Persistant connections cant disconnect
mysql_close($this->_resource);
}
function useDB($db)
{
mysql_select_db($db, $this->_resource);
}
function query($id, $q)
{
$err=null;
$return=true;
$$id = array($q);
$tmp = mysql_query($q, $this->_resource) or $err=mysql_error() and $return=false;
array_push($$id, $tmp);
array_push($$id, $err);
$this->_queries[$id] = $$id;
unset($$id);
return $return;
}
function get_assoc($id)
{
return mysql_fetch_assoc($this->_queries[$id][1]);
}
function insert($id, $table, $arr_columns, $arr_values)
{
$arr_columns = implode(", ", $arr_columns);
$arr_values = implode("' , '", $arr_values);
$q = "INSERT INTO $table ($arr_columns) VALUES ('$arr_values')";
echo $q;
return $this->query($q);
}
function get_error($id)
{
return $this->_queries[$id][2];
}
}
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
//The fun starts here everthing above was just the class
//the only real code is down below:P
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
// server user password database presistant
$sql = new MysqlConnection('server', 'user', 'pass', 'database', true);
// database
//$sql->useDB('newdb');
// identifier query
$sql->query('committees', 'SELECT * FROM committee_master');
// identifier
print_r($sql->get_assoc('committees'));
// select columns
$arr_a = array("username", "password", "account_type", "committee_Id", "aone");
// select column values
$arr_b = array("testusername", "password", "test_acc", "200", "210");
// identifier table columns values
if ($sql->insert('test insert', 'users', $arr_a, $arr_b))
{
echo "<br>done!</br>";
}
else
{
echo "<br>ERROR!</br>";
// identifier
echo $sql->get_error('test insert');
}
$sql->disconnect();
?>
|

11-11-2004
|
 |
Accomplished Graduate Student
|
|
Join Date: Sep 2004
Posts: 350
Rep Power: 6
|
|
Re: Mysql Library (class to make it easier)
|
|
nitin, wouldn't it be a better idea to modify that so that you can include a small file like "defaults.inc.php" and do stuff like $sql->set_default_server("localhost"); etc. so that they just have to do connect or disconnect? Good to see that you can toggle persistent connections with a variable, but again this ought to be a variable that can be defaulted and changed -- what if a server is being hammered and you figure persistent connections could correct that? Bam, switch the config and all future connections stay connected.
|
 |
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|