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!
<?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.
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.
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.
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.
<?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);
}
}
?>
$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.
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.