Hi board.
I'm tweaking my scripts at the moment - implementing a better db config file etc. However I'm having trouble with returning the mysql_result($query,0,0) with the db config.
The db config is fairly simple - I invoke $myDB->query("SELECT....") to run,
and a set of functions return mysql_fetch_obj, array etc.
Below is the db config..
<?php
// Class: DB
// MySQL database connection wrapper
class DB
{
// connection parameters
var $host = '';
var $user = '';
var $pass = '';
var $database = '';
var $persistent = false;
var $link = NULL;
var $result = false;
// Constructor
// Initializes member variables
function DB($host, $user, $pass, $database='', $persistent = false) {
$this->host = $host;
$this->user = $user;
$this->pass = $pass;
$this->database = $database;
$this->persistent = $persistent;
}
// Method: connect()
// Establishes a connection to the database server. Returns true or false
// depending on whether the operation was successful or not.
function connect() {
// Choose the appropriate connect function
if ($this->persistent) {
$func = 'mysql_pconnect';
} else {
$func = 'mysql_connect';
}
// Connect to the MySQL server and report any errors
$this->link = $func($this->host, $this->user, $this->pass);
if (!$this->link) {
return false;
}
// If a database has been specified then attempt to seelct it
if ($this->database != "") {
if (!@mysql_select_db($this->database, $this->link)) {
return false;
}
}
// Return success to calling function
return true;
}
// Method: close()
// Terminates the connection to the database. Returns true if the
// connection was terminated successfully, and false otherwise.
function close() {
return (@mysql_close($this->link));
}
// Method: error()
// Returns the error message, is any, from the last database operation
// performed.
function error() {
return (mysql_error());
}
// Method: query()
// Standard query function which returns true if the query was successfully
// executed and false otherwise.
function query($sql = '') {
$this->result = @mysql_query($sql, $this->link);
return ($this->result != false);
}
// Method: queryFirst()
// A specialised query function that executes the query and then returns
// the first row from the result set. Used for SELECT queries only.
function queryFirst($sql) {
// Execute query
$this->result = @mysql_query($sql, $this->link);
// If query failed then exit
if (!$this->result) {
return false;
}
// Fetch first row from result set as an associative array
$returnArray = mysql_fetch_array($this->result, MYSQL_ASSOC);
// Return array to calling function
return $returnArray;
}
// Method: affectedRows()
// Invokes the standard mysql_affected_rows() function to find out the
// number of rows affected by the previous query.
function affectedRows() {
return (@mysql_affected_rows($this->link));
}
// Method: numRows()
// Invokes the standard mysql_num_rows() function and returns the number of
// rows returned by the previous SELECT query.
function numRows() {
return (@mysql_num_rows($this->link,0,0));
}
// Method: fetchObject()
// Returns the next row from the result set as an object
function fetchObject() {
return (@mysql_fetch_object($this->result, MYSQL_ASSOC));
}
// Method: fetchArray()
// Returns the next row from the result set as a numerically indexed array
function fetchArray() {
return (@mysql_fetch_array($this->result, MYSQL_NUM));
}
// Method: fetchAssoc()
// Returns the next row from the result set as an associative array
function fetchAssoc() {
return (@mysql_fetch_assoc($this->result));
}
// Method: firstResult()
// Returns mysql_result(..,0,0) - when one result is known.
function firstResult() {
return (@mysql_result($this->sql,0,0));
}
// Method: freeResult()
// Uses the standard mysql_free_result() function to free the current
// result set.
function freeResult() {
return (@mysql_free_result($this->result));
}
// Method: insertID()
// Returns the AUTO_INCREMENT column value for the row added by the
// previous query.
function insertID() {
return (@mysql_insert_id($this->link));
}
}
?>
I tried to add the 'firstResult' function to return mysql_result($...,0,0) but it will not work.
Even when I try to invoke it without using this function, ie. in a page as @mysql_result($query,0,0) - nothing gets returned.
Can anyone help.
Thanks in advance.
Ru