Why do this?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Why do this?

Post by s.dot »

Code: Select all

<?php

/*
* MySQL Database Connection Class
* Date: 8-18-2006
* Author: scottayy@gmail.com
*/

class db
{
	var $host;
	var $user;
	var $pass;
	
	function connect($host,$user,$pass)
	{	
		$this->host = $host;
		$this->user = $user;
		$this->pass = $pass;
		
		return mysql_connect($this->host,$this->user,$this->pass);
	}
Is there any point in setting the class variables to that?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

With the code, as-is, the object's properties are useless as they'll be reset everytime connect() is called.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Well I'm wondering how I would pass that as a resource to the select_db function?

Code: Select all

function select_db($db_name)
{
   return mysql_select_db($db_name, <<resource link from connect()>>
}
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

scottayy wrote:Well I'm wondering how I would pass that as a resource to the select_db function?

Code: Select all

function select_db($db_name)
{
   return mysql_select_db($db_name, <<resource link from connect()>>
}
You forgot a bracket.

Just kidding :P
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

scottayy wrote:Well I'm wondering how I would pass that as a resource to the select_db function?
You don't pass it. It should be an internal only property of the DB object.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Suppose I could do it like this..

Code: Select all

function connect($host,$user,$pass,$db_name)
	{	
		mysql_select_db($db_name,$this->get_resource($host,$user,$pass));
	}
	
	function get_resource($host,$user,$pass)
	{
		return mysql_connect($host,$user,$pass);
	}
Seems kinda ugly though. I should probably look at an existing class to get a feel how it works before attempting to write my own. =/
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

feyd wrote:
scottayy wrote:Well I'm wondering how I would pass that as a resource to the select_db function?
You don't pass it. It should be an internal only property of the DB object.
Ah!
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

So something like this

Code: Select all

<?php

class db
{
	var $resource;
	
	function connect($host,$user,$pass)
	{	
		$this->resource = mysql_connect($host,$user,$pass);
	}
	
	function select_db($db_name)
	{
		mysql_select_db($db_name,$this->resource);
	}
	
}

?>
I could then connect by doing

Code: Select all

$db = new db();
$db->connect('localhost','user','pass');
$db->select_db('my_db');
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Image
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Excellent. That is typically called a DB Connection class. Usually is also contains methods to execute a query, get post query values like last insert ID, begin and commit transactions, and get error number and message. The query method usually returns an object for select queries that can be a simple Record Set or an Active Record object which adds business logic and sometimes some O/RM.
(#10850)
Post Reply