Location: PHPKode > scripts > phpDB > phpdb/example_MySQL.php
<?php
#===========================================================================
#= Script : Database Access Class
#= File   : example_MySQL.php
#= Version: 0.2
#= Author : Mike Leigh
#= Email  : hide@address.com
#= Website: http://www.mikeleigh.com/scripts/databaseaccess
#= Support: http://www.mikeleigh.com/forum
#===========================================================================
#= Copyright (c) 2005 Mike Leigh
#= You are free to use and modify this script as long as this header
#= section stays intact
#= This file is part of DownloadCounter.
#=
#= This program is free software; you can redistribute it and/or modify
#= it under the terms of the GNU General Public License as published by
#= the Free Software Foundation; either version 2 of the License, or
#= (at your option) any later version.
#=
#= This program is distributed in the hope that it will be useful,
#= but WITHOUT ANY WARRANTY; without even the implied warranty of
#= MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#= GNU General Public License for more details.
#=
#= You should have received a copy of the GNU General Public License
#= along with DownloadCounter; if not, write to the Free Software
#= Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#===========================================================================
include('database.class.php');

//create a table named user on your test database to use this example

$attribs = array('type' => 'MySQL');

$db = new phpDB(array('type' => 'MySQL'));
$db->setAttribute('host', '127.0.0.1');
$db->setAttribute('username', 'root');
$db->setAttribute('password', '1234');
$db->setAttribute('database', 'test');

$db->connect();
$db->selectDatabase();
$db->execute("truncate table user");
$db->execute("insert into user (username,password) values ('user1','".md5(time()*rand())."')");
$db->execute("insert into user (username,password) values ('user2','".md5(time()*rand())."')");
$db->execute("insert into user (username,password) values ('user3','".md5(time()*rand())."')");
$db->fetch("select * from user");

//this will work if the resultset is associative
if($db->getAttribute('fetch') == 'associative') {
	$array = $db->getResultArray();
	print "<table border='1' cellpadding='2' cellspacing='2'>";
	print "<tr><th colspan='2'>Associative</th></tr>";
	print "<tr><th>Username</th><th>Password</th></tr>";
	for($i = 0; $i <= count($array) - 1; $i ++) {
		print "<tr><td>".$array[$i]['username']."</td><td>".$array[$i]['password']."</td></tr>";
	}
}

//this will work if the resultset is numeric
if($db->getAttribute('fetch') == 'numeric') {
	$array = $db->getResultArray();
	print "<table border='1' cellpadding='2' cellspacing='2'>";
	print "<tr><th colspan='2'>Numeric</th></tr>";
	print "<tr><th>0</th><th>1</th></tr>";
	for($i = 0; $i <= count($array) - 1; $i ++) {
		print "<tr><td>".$array[$i][0]."</td><td>".$array[$i][1]."</td></tr>";
	}
	print "</table>";
}

//this will work if the resultset is both
if($db->getAttribute('fetch') == 'both') {
	$array = $db->getResultArray();
	print "<table border='1' cellpadding='2' cellspacing='2'>";
	print "<tr><th colspan='2'>Associative</th></tr>";
	print "<tr><th>Username</th><th>Password</th></tr>";
	for($i = 0; $i <= count($array) - 1; $i ++) {
		print "<tr><td>".$array[$i]['username']."</td><td>".$array[$i]['password']."</td></tr>";
	}
	print "</table>";
	print "<table border='1' cellpadding='2' cellspacing='2'>";
	print "<tr><th colspan='2'>Numeric</th></tr>";
	print "<tr><th>0</th><th>1</th></tr>";
	for($i = 0; $i <= count($array) - 1; $i ++) {
		print "<tr><td>".$array[$i][0]."</td><td>".$array[$i][1]."</td></tr>";
	}
	print "</table>";
}

$db->close();

function showError() {
	global $db;
	$status = $db->getStatus();
	if($status == false) {
		print "<p>Error: ".$db->getMessage()."</p>";
	}
}

function showAttributes() {
	global $db;
	print "<pre>";
	print_r($db->attributes);
	print "</pre>";
}
?>
Return current item: phpDB