PHP: reusing database class
Posted
by citricsquid
on Stack Overflow
See other posts from Stack Overflow
or by citricsquid
Published on 2010-03-07T23:58:18Z
Indexed on
2010/03/08
0:01 UTC
Read the original article
Hit count: 133
Hi,
I built a class that allows me to do:
$db->query($query);
and it works perfectly, although if I want to do:
$db->query($query);
while($row = $db->fetch_assoc()){
$db->query($anotherquery);
echo $db->result();
}
it "breaks" the class. I don't want to constantly have to redeclare my class (eg: $seconddb = new database()), is there a way to get around this? I want to be able to reuse $db within $db, without overwriting the "outside" db. currently I'm create an array of data (from db->fetch_assoc() then doing a foreach and then doing the db call inside that:
$db->query('SELECT * FROM table');
while($row = $db->fetch_assoc()){
$arr[] = $row;
}
foreach($arr as $a){
$db->query(); // query and processing here
}
Is this the best method or am I missing the obvious? Should I consider passing a connection link ID with the database connection?
© Stack Overflow or respective owner