<?PHP
// MYSQL_RECORDSET: SQL selection framework for MYSQL_DB
// Release 1.1 - 06/08/2002
// Released under GNU GPL (see LICENSE for details)
class mysql_recordset {
// Init variables required
VAR $connobj = "" ;
VAR $sqlstring = "" ;
// Data related variables
VAR $rs = 0 ;
VAR $row = 0 ;
VAR $recordcount = 0 ;
VAR $EOF = true ;
// Private methods
function get_recordset()
{
// Determine if there's an open database a valid sql command
if ($GLOBALS[$this->connobj]->db && $this->sqlstring!="") {
$this->rs = mysql_query($this->sqlstring,$GLOBALS[$this->connobj]->db) ;
$GLOBALS[$this->connobj]->error_report() ;
// Determine if there's a valid result
if ($this->rs) {
// Valid recordset
$this->EOF = false ;
$this->recordcount = mysql_num_rows($this->rs) ;
$GLOBALS[$this->connobj]->msg = date("d/m/Y - H:i:s") . " - OPERATION O.K.: Executed " . $this->sqlstring ." got " . $this->recordcount . " rows" . "\r\n";
} else {
// Not a valid recordset
$this->recordcount = 0 ;
$this->EOF = true ;
$GLOBALS[$this->connobj]->msg = date("d/m/Y - H:i:s") . " - OPERATION FAILED: Executed " . $this->sqlstring . " got " . $GLOBALS[$this->connobj]->error_level . " " . $GLOBALS[$this->connobj]->error_desc . "\r\n" ;
}
} else {
// No db or no SQL command
$this->recordcount = 0 ;
$this->EOF = true ;
$GLOBALS[$this->connobj]->msg = date("d/m/Y - H:i:s") . " - OPERATION CANCELED: No database open OR no SQL command provided" . "\r\n" ;
}
$GLOBALS[$this->connobj]->debug() ;
}
// Fetch a row into an array. NOTES: fields with same name get no table.name key in the array USE ALIAS
function get_row()
{
// Determine if we have a valid result
if ($this->rs && $GLOBALS[$this->connobj]->db) {
// Fetch the result in the array
$this->row = mysql_fetch_array($this->rs) ;
$GLOBALS[$this->connobj]->error_report() ;
if ($this->row) {
$this->EOF = false ;
} else {
$this->EOF = true ;
}
} else {
// No valid result open
$GLOBALS[$this->connobj]->msg = date("d/m/Y - H:i:s") . " - OPERATION FAILED: No database open or no recordset open" . "\r\n" ;
}
}
// --------- Public interface ---------
// Execute a SQL query
function query()
{
$this->get_recordset() ;
return $this->recordcount ;
}
// Fetch next row in the recordset
function movenext()
{
$this->get_row() ;
return $this->EOF ;
}
// Echo a field value to screen
function field($campo)
{
echo stripslashes($this->row[$campo]) ;
}
// Return a field value to php
function value($campo)
{
return stripslashes($this->row[$campo]) ;
}
// Constructor
function mysql_recordset($connobjname,$sqlcommand)
{
$this->connobj = $connobjname ;
$this->sqlstring = $sqlcommand ;
}
// Destroy intermediate memory used by the result
function clear_recordset()
{
$this->sqlstring="" ;
if ($this->rs) {
mysql_free_result($this->rs) ;
}
}
// Move to a given row number (0 based)
function move_to($rnumber)
{
if ($this->rs) {
if (!mysql_data_seek($this->rs,$rnumber)) {
$GLOBALS[$this->connobj]->error_report() ;
$GLOBALS[$this->connobj]->msg = date("d/m/Y - H:i:s") . " - OPERATION FAILED: Could not move to record " . $rnumber . " " . $GLOBALS[$this->connobj]->error_level . " " . $GLOBALS[$this->connobj]->error_desc . "\r\n";
} else {
$GLOBALS[$this->connobj]->msg = date("d/m/Y - H:i:s") . " - OPERATION O.K.: Result pointer moved to " . $rnumber . "\r\n" ;
}
} else {
$GLOBALS[$this->connobj]->msg = date("d/m/Y - H:i:s") . " - OPERATION FAILED: No result open" . "\r\n" ;
}
$GLOBALS[$this->connobj]->debug() ;
}
// Go to record 0
function movefirst()
{
$this->move_to(0) ;
}
// Go to last record
function movelast()
{
$this->move_to($this->recordcount-1) ;
}
}
?>