VB icon

MySQL class

Email
Submitted on: 1/1/2015 4:06:00 AM
By: Dennis Wrenn (from psc cd)  
Level: Intermediate
User Rating: By 3 Users
Compatibility: PHP 3.0, PHP 4.0
Views: 1189
 
     a class to aid in the creation of MySQL powered PHP applications. Most functions are just wrappers for MySQL functions, but has a little more error checking, so you don't screw something up, as I have many... many times.... :-P
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: MySQL class
// Description:a class to aid in the creation of MySQL powered PHP applications. Most functions are just wrappers for MySQL functions, but has a little more error checking, so you don't screw something up, as I have many... many times.... :-P
// By: Dennis Wrenn (from psc cd)
//**************************************

<?php
class db
{
	var $database = ""; //dbname
	var $linkid = 0;
	var $queryid = 0;
	var $record = array();
	var $server = "localhost"; //servername
	var $user = "root"; //username
	var $password = ""; //password
	function connect() 
	{
	//connects to database
		if ($this->link_id == 0) 
		{
			if ($this->password=="")
			{
				$this->link_id=mysql_pconnect($this->server,$this->user);
			}
			else
			{
				$this->link_id=mysql_pconnect($this->server,$this->user,$this->password);
			}
			if ($this->database!="")
			{
				mysql_select_db($this->database, $this->link_id);
			}
		}
	}
	function selectdb($db = "")
	{
	//selects db
		if ($db != "")
		{
			$this->database = $db;
		}
		mysql_select_db($this->database, $this->linkid
	}
	function query($qs)
	{
	//query function
		$this->queryid = mysql_query($qs, $this->linkid);
		return $this->queryid;
	}
	function fetcharray($qid = -1)
	{
	//fetches single row from result of query.
		if ($qid != -1)
		{
			$this->queryid=$qid;
		}
		$this->record = mysql_fetch_array($this->queryid);
		return $this->record;
	}
	function freeresult($qid = -1) 
	{
	//incase you're using too much memory, this frees some
		if ($qid!=-1)
		{
			$this->queryid=$qid;
		}
		return @mysql_free_result($this->queryid);
	}
	function queryfirst($qs)
	{
	//gets first row of result of query.
		$this->query($qs);
		$retarray=$this->fetcharray($this->queryid);
		$this->freeresult($this->$queryid);
		return $retarray;
	}
	function data_seek($pos,$qid = -1)
	{
	//goes to a specified row for use in fetcharray()
		if ($qid != -1)
		{
			$this->queryid=$qid;
		}
		$status = mysql_data_seek($this->queryid, $pos);
		return $status;
	}
	function numrows($qid = -1)
	{
	//gets number of rows
		if ($qid!=-1)
		{
			$this->queryid=$qid;
		}
		return mysql_num_rows($this->queryid);
	}
	function insert_id()
	{
	//gets number from last auto_insert field
		return mysql_insert_id($this->linkid);
	}
}
?>


Other 3 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.