Problem with sql class and mysql_fetch_row

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ron_j_m
Forum Commoner
Posts: 35
Joined: Wed Feb 02, 2005 8:56 pm

Problem with sql class and mysql_fetch_row

Post by ron_j_m »

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

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);
  }
}
?>
Here is the some code to retrieve and display the results.
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]
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

You are running this code

Code: Select all

function result() {
    $this->connect();
    $result = @mysql_fetch_row($this->query());
    return($result);
  }
and you are expecting more than 1 row?
To build an array your going to have to loop through your results adding to the array...
this will include using while() and mysql_fetch_assoc()
Post Reply