help in db wrapper class ??
Posted: Sun May 20, 2007 4:43 am
I had the following db wrapper class:
db.class.php
when i tried to run another query in the loop of one query , it only shows the results of one. For example:
In above it returns the field names of only one table.
but if we create another instance of Db class inside the loop and use it , it works.
What i want ?
- i want to modify the db wrapper class to avoid such case ie no need to create another object inside the loop for multilevel queries.
thanks in advance to all of you
db.class.php
Code: Select all
<?php
class Db{
var $linkID;
var $queryID;
/* Open Database linkID */
function Db(){
$this->linkID = mysql_connect(DB_HOST, DB_USER, DB_PASS);
mysql_select_db(DB_NAME);
}
}
function doQuery($sql){
$this->queryID = @mysql_query($sql,$this->linkID);
return $this->queryID;
}
/* Fetch Data as Array */
function doFetchArr(){
return mysql_fetch_array($this->queryID);
}
function doFetchNum(){
return mysql_fetch_row($this->queryID);
}
/* Return the number of fields in the most recently retrieved result */
function getNumCols(){
return mysql_num_fields($this->queryID);
}
/* Free the Result */
function doFreeResult(){
return mysql_free_result($this->queryID);
}
/* Close Database linkID */
function doClose(){
mysql_close($this->linkID);
}
}
?>Code: Select all
require_once("db.class.php");
$Db = new Db();
$sql = "SHOW TABLES FROM ".DB_NAME;
$result = $Db->doQuery($sql);
while ($row = $Db->doFetchNum()) {
//$Db1 = new Db();
echo "Table: {$row[0]}<br />";
$sql2 = "SELECT * FROM {$row[0]}";
$result2 = $Db->doQuery($sql2);
$num_fields = $Db->getNumCols();
//$Db->doFreeResult();
for($i = 0; $i < $num_fields; $i++){
echo mysql_field_name($result2, $i)."<br />";
}
}but if we create another instance of Db class inside the loop and use it , it works.
What i want ?
- i want to modify the db wrapper class to avoid such case ie no need to create another object inside the loop for multilevel queries.
thanks in advance to all of you