...

View Full Version : more classes..



johnrau
06-06-2004, 03:07 PM
hey

heres the deal- im making a website that has drink recipes on it, and it uses a class to connect to the db, so i figured why not use a class to grab and modify drinks

so, for starters, heres my db class:


class Database{

// private instant variables
var $dbConnectionID;
var $queryID;
var $record;
var $host;
var $database;
var $user;
var $password;

/*
constructor
connect to datbase server and select specified database
*/
function Database($host=MYSQL_HOST, $db=MYSQL_MAIN_DB, $user=MYSQL_USER, $pwd=MYSQL_PASS){
$this->host = $host;
$this->database = $db;
$this->user = $user;
$this->password = $pwd;
$this->connect();
}


/*
private method
used internally to generate dbConnectionID
*/
function connect(){
$this->dbConnectionID = @mysql_pconnect($this->host, $this->user, $this->password);
if(!$this->dbConnectionID){
echo(mysql_errno().":".mysql_error());
exit;
}
else{
$status = @mysql_select_db($this->database, $this->dbConnectionID);
if(!$status){
echo(mysql_errno().":".mysql_error());
exit;
}
}
}


// public methods

function query($sql){
// connect to db incase connection id is not set
if(empty($this->dbConnectionID))
$this->connect();
$this->queryID = @mysql_query($sql, $this->dbConnectionID);

// handle error
if(!$this->queryID){
echo(mysql_errno().":".mysql_error());
exit;
} else {
$this->nextRecord(); // had to add this in so you dont have to manually skip to the first record, (1 not 0) -jr
}
}

function nextRecord(){
$this->record = @mysql_fetch_array($this->queryID);
$status = is_array($this->record);
return($status);
}

function numRows(){
$rows = @mysql_num_rows($this->queryID);
return($rows);
}

// get record field value from the current record pointed by $record
function getField($field){
return($this->record[$field]);
}
}


...and heres my drink class...



class drink {

var $id;
var $title;
var $ingredients;
var $directions;
var $user;
var $submitted;
var $rating;
var $ratings;
var $views;
var $rating_addrs;
var $online;

function select($tmp_id) {
global $db;
$this->id = $tmp_id;
unset($tmp_id);
if ($db->query("SELECT * FROM tt_drinks WHERE id = ".$this->id)) {
return true;
$this->id = $db->getField("id");
$this->title = $db->getField("title");
$this->ingredients = $db->getField("ingredients");
$this->directions = $db->getField("directions");
$this->user = $db->getField("user");
$this->submitted = $db->getField("submitted");
$this->rating = $db->getField("rating");
$this->ratings = $db->getField("ratings");
$this->views = $db->getField("views");
$this->rating_addrs = $db->getField("rating_addrs");
$this->online = $db->getField("online");
} else {
// debug
echo mysql_error();
echo "oops";
return false;
}
}

function delete($id) {
global $db;
if (@$db->query("DELETE FROM tt_drinks WHERE id = ".$id)) {
return true;
} else {
return false;
}
}

function add() {
global $db;
if (@$db->query("INSERT INTO tt_drinks VALUES(".
$this->id.",".
$this->title."','".
$this->ingredients."','".
$this->directions."','".
$this->user."','".
$this->submitted."','".
$this->rating."','".
$this->ratings."','".
$this->views."','".
$this->rating_addrs."','".
$this->online."')")) {
return true;
} else {
return false;
}
}

function modify() {
global $db;
// delete the old entry
$this->delete($this->id);
// add the new one, with old ID
if ($this->add()) {
return true;
} else {
return false;
}
}

}


by looking at the code you can pretty much guess how it works

and heres my file thats supposed to show some output:



// start our default (main) database
require("inc/database.class.php"); // database connector class
$db = new Database;

// includes - seperate files for inclusion, classes, etc
require("inc/drink.class.php"); // drink operations class

$drink = new drink;
$drink->select(453);


when i run that code itoutpust my error message "oops"

ive tried running that individual query alone $db->query("SELECT * FROM tt_drinks WHERE id = ".$this->id) and it runs fine!

any ideas why this may not be working (ps its no a mysql syntax error cuz mysql_error() doesnt output anything

thanks in advance!
-john

firepages
06-07-2004, 03:31 AM
Hi , your DB class returns nothing so $db is not a resource...
add

<?
......
$this->connect();
return $this->dbConnectionID;
}
?>
to the Database constructor



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum