Topic: MySQL 101 - basics for the beginner
I've decided to take the plunge and start using MySQL databases in my websites. (Other than for pre-packaged applications like PunBB - I'm already doing that.) I've read a couple tutorials and I have a basic beginner-level understanding.
The first step is to connect. I want to organize the files in a "proper" way, so I studied how PunBB does it:
index.php
> include/common.php
> > config.php
> > include/functions.php
> > include/dblayer/common_db.php
> > > include/dblayer/mysql.php
I think I understand everything except for what's going on in the last file. The 'common_db.php' points to another file depending on the type of database being used. Simple enough. So then you go to the 'mysql.php' file and there's about 100 lines of code that make no sense to me.
It looks like I need to, "Load the appropriate DB layer class." What's a simple way to do this? (as compared to the way it's done in the mysql.php file)
Here's an excerpt from mysql.php as a reminder of what it contains:
// Make sure we have built in support for MySQL
if (!function_exists('mysql_connect'))
exit('This PHP environment doesn\'t have MySQL support built in.');
//
// Return current timestamp (with microseconds) as a float.
//
if (defined('PUN_SHOW_QUERIES'))
{
function get_microtime()
{
list($usec, $sec) = explode(' ', microtime());
return ((float)$usec + (float)$sec);
}
}
class DBLayer
{
var $prefix;
var $link_id;
var $query_result;
var $row = array();
var $saved_queries = array();
var $num_queries = 0;
function DBLayer($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect)
{
$this->prefix = $db_prefix;
if ($p_connect)
$this->link_id = @mysql_pconnect($db_host, $db_username, $db_password);
else
$this->link_id = @mysql_connect($db_host, $db_username, $db_password);
if ($this->link_id)
{
if (@mysql_select_db($db_name, $this->link_id))
return $this->link_id;
else
error('Unable to select database. '.mysql_error(), __LINE__, __FILE__);
}
else
error('Unable to connect to MySQL server. '.mysql_error(), __LINE__, __FILE__);
}
function query($sql = '', $transaction = 0)
{
unset($this->query_result);
if ($sql != '')
{
if (defined('PUN_SHOW_QUERIES'))
$q_start = get_microtime();
$this->query_result = @mysql_query($sql, $this->link_id);
}
if ($this->query_result)
{
if (defined('PUN_SHOW_QUERIES'))
$this->saved_queries[] = array($sql, sprintf('%.5f', get_microtime() - $q_start));
++$this->num_queries;
unset($this->row[$this->query_result]);
return $this->query_result;
}
else
return ($transaction == PUN_TRANS_END) ? true : false;
}
function result($query_id = 0, $row = 0){}
function fetch_array($query_id = 0){}
function fetch_assoc($query_id = 0){}
function fetch_row($query_id = 0){}
function num_rows($query_id = 0){}
function affected_rows(){}
function insert_id(){}
function get_num_queries(){}
function get_saved_queries() {}
function free_result($query_id = false) {}
function error() {}
function close() {}
}
One of these days, I'm going to upgrade my forum.
D9r.com - homepage
Tucker & DeKalb County, Georgia -- news, events, zoning, politics, directory
HiveMinds.info - International Web Developer Forum (lots of smart folks here)