Problem with sql class and mysql_fetch_row
Posted: Thu Sep 29, 2005 2:47 pm
I am using this class to fetch a row from a MySQL database. The problem comes when I try to loop through the array using foreach, or while.
It just returns the first entry in the row. Everything works fine when I dont use the class and just code it into my script using mysql_fetch_row.
Heres the class
dbclass.php
Here is the some code to retrieve and display the results.
index.php
It just returns the first entry in the row. Everything works fine when I dont use the class and just code it into my script using mysql_fetch_row.
Heres the class
dbclass.php
Code: Select all
class db {
var $database = db_database;
var $run;
var $standby = false;
function connect() {
$connect = @mysql_pconnect(db_hostname, db_username, db_password) or die("Failed to connect to MySQL host.<br>". mysql_error() ."<br><br><strong>Line:</strong> ". __LINE__ ."<br><strong>File:</strong> ". __FILE__);
$this->select();
return($connect);
}
function select() {
@mysql_select_db($this->database) or die("Failed to select mysql DB {$this->database}.<br>". mysql_error() ."<br><br><strong>Line:</strong> ". __LINE__ ."<br><strong>File:</strong> ". __FILE__);
}
function close() {
@mysql_close($this->connect());
}
function query() {
$this->connect();
$query = @mysql_query($this->run) or die("Failed to query the database.<br>". mysql_error() ."<br><br><strong>Line:</strong> ". __LINE__ ."<br><strong>File:</strong> ". __FILE__);
if ($this->standby == false) {
$this->close();
}
return($query);
}
function num_results() {
$this->connect();
$total = @mysql_num_rows($this->query());
return($total);
}
function result() {
$this->connect();
$result = @mysql_fetch_row($this->query());
return($result);
}
}
?>index.php
Code: Select all
<?
include "dbclass.php";
$db = &new db;
$db->run = "SELECT row FROM table";
$db->database = "mydatabase";
$result = $db->result();
foreach ($result as $domain){
echo $domain;
echo "<br>";
}
?>
I've tried everything I can think of. Ive tried while loops foreach loops and both together with no luck. It just sends back and array with only 1 entry. Like I said before, it works fine if I use a while loop with mysql_fetch_row in index.php.
Any Ideas?
Wrench[/b]