Hi, Im new to classes and was wondering if anyone colud show me the best way of returning the results from a MYSQL_FETCH_ARRAY() from a class.
The class should be able to work with any table.
I have it returning through an array and then extracting it once its returned. But I am looking for a way of doing like the 1 by 1 method below.
while($row = msql_fetch_array($result)) {
echo $row["client_id"];
echo $row["fullname"];
}
I dont want to do it in the method I have used below as results from a big query would make a huge array, which would use tie up server memory.
Hope this makes sence
Below is my code.
This is index.php:-
<?
include ("connect.obj.php");
$db = new database();
$db->serverConnection();
$db->useDB("towns");
$db->sql("SELECT DISTINCT * FROM towns ORDER BY county ASC LIMIT 10");
$result=$db->fetchData();
extract($result[1]);
echo $county;
?>
<html>
<body>
<form action="<? echo $php_self ?>">
</form>
</body>
</html>
################################################
And here is my class:-
<?
class database{
var $con;
var $useDb;
var $query;
var $sql;
var $host = "TOSH";
var $user = "root";
var $password = "";
var $value;
var $dbName; // Database name
function serverConnection(){
$this->con =@MYSQL_CONNECT($this->host,$this->user,$this->password) OR DIE (@$this->_errors("There was a problem connecting to the database server"));
}
function useDb($dbName){
$this->useDb=@MYSQL_SELECT_DB($dbName) OR DIE (@$this->_errors("Unable to select the database $dbName"));
}
function sql($sql){
$this->query=MYSQL_QUERY($sql) OR DIE (@$this->_errors("Unable to run query $sql"));
}
function fetchData(){
$this->numRows=@MYSQL_NUM_ROWS($this->query) OR DIE (@$this->_errors("Unable to count number of rows"));
if ($this->numRows){
while($val = @MYSQL_FETCH_ARRAY($this->query)){
$arr[]=$val;
}
return $arr;
} else {
return false;
}
}
function _errors($txt){
echo $txt."<br>";
}
}
?>