I have used PHP for several years now, and OOP, classes and such are not an alien concept to me, but for some reason, there is something (not) happening that i just can get my head around, its 1am here now, and it might have something to do with it but please.. someone put me out of my midnight misery !!
I have a PHP DB Class, and a populated DB, and several functions at play here.. I KNOW there are over 50 lines in the database that shoud be returned, but i keep getting just one !!
Heres my class for DB Connection..
Class DbConnect {
var $host = 'localhost';
var $user = 'user';
var $password = 'pass';
var $database = 'db';
var $persistent = false;
var $conn = NULL;
var $result= false;
var $error_reporting = false;
/*constructor function this will run when we call the class */
function DbConnect () {
$host = 'localhost';
$user = 'user';
$password = 'pass';
$database = 'db';
$persistent = false;
$error_reporting = true;
$this->host = $host;
$this->user = $user;
$this->password = $password;
$this->database = $database;
$this->persistent = $persistent;
$this->error_reporting = $error_reporting;
}
function open() {
if ($this->persistent) {
$func = 'mysql_pconnect';
} else {
$func = 'mysql_connect';
}
/* Connect to the MySQl Server */
$this->conn = $func($this->host, $this->user, $this->password);
if (!$this->conn) {
return false;
}
/* Select the requested DB */
if (@!mysql_select_db($this->database, $this->conn)) {
return false;
}
return true;
}
/*close the connection */
function close() {
return (@mysql_close($this->conn));
}
/* report error if error_reporting set to true */
function error() {
if ($this->error_reporting) {
return (mysql_error()) ;
}
}
function query($sql) {
$this->result = @mysql_query($sql, $this->conn);
return($this->result != false);
}
function affectedrows() {
return(@mysql_affected_rows($this->conn));
}
function numrows() {
return(@mysql_num_rows($this->result));
}
function fetchobject() {
return(@mysql_fetch_object($this->result, MYSQL_ASSOC));
}
function fetcharray() {
return(mysql_fetch_array($this->result));
}
function fetchassoc() {
return(@mysql_fetch_assoc($this->result));
}
function freeresult() {
return(@mysql_free_result($this->result));
}
}
ok, and here’s my actual query function…
function getAllProperties() {
$propertyArray = array();
$db = new DbConnect();
$db->open() or die($db->error());
$db->query("select * from properties");
$details=$db->fetchassoc();
// UNCOMMENT FOR DEBUG ---->> var_dump($details);
$a=0;
foreach($details as $field=>$value) {
if($field == 'pid') {
$propertyArray[$a] = getPropertyDetails($value);
$a++;
}
}
$db->close();
return $propertyArray;
}
function getPropertyDetails($pid) {
$propertyArray = array();
$propertyDetails = array();
$propertyFeatures = array();
$propertyImages = array();
$propertyReviews = array();
$propertyBookings = array();
$db = new DbConnect();
$db->open() or die($db->error());
// property details
$db->query("select * from properties where pid = $pid");
$a = 0;
while($details=$db->fetchassoc()) {
$propertyDetails[$a]['pid'] = $details['pid'];
$propertyDetails[$a]['name'] = $details['name'];
$propertyDetails[$a]['description'] = $details['description'];
$propertyDetails[$a]['rooms'] = $details['rooms'];
$propertyDetails[$a]['baths'] = $details['baths'];
$propertyDetails[$a]['sleeps'] = $details['sleeps'];
$propertyDetails[$a]['rental'] = $details['rental'];
$propertyDetails[$a]['sale'] = $details['sale'];
$propertyDetails[$a]['forsale'] = $details['forsale'];
$a++;
}
// property details
$db->query("select * from images where pid = $pid order by weight asc");
$a = 0;
while($details=$db->fetchassoc()) {
$propertyImages[$a]['src'] = $details['img'];
$propertyImages[$a]['thumb'] = $details['thumb'];
$propertyImages[$a]['alt'] = $details['description'];
$propertyImages[$a]['weight'] = $details['weight'];
$propertyImages[$a]['iid'] = $details['iid'];
$propertyImages[$a]['pid'] = $details['pid'];
$a++;
}
// property details
$db->query("select * from property_features pf join features f on f.fid = pf.fid where pid = $pid");
$a = 0;
while($details=$db->fetchassoc()) {
$propertyFeatures[$a]['fid'] = $details['fid'];
$propertyFeatures[$a]['name'] = $details['name'];
$propertyFeatures[$a]['description'] = $details['description'];
$propertyFeatures[$a]['img'] = $details['img'];
$propertyFeatures[$a]['pid'] = $details['pid'];
$a++;
}
// property details
$db->query("select * from testimonials where pid = $pid");
$a = 0;
while($details=$db->fetchassoc()) {
$propertyReviews[$a]['tid'] = $details['tid'];
$propertyReviews[$a]['pid'] = $details['pid'];
$propertyReviews[$a]['by'] = $details['by'];
$propertyReviews[$a]['body'] = $details['body'];
$propertyReviews[$a]['stars'] = $details['stars'];
$propertyReviews[$a]['cid'] = $details['cid'];
$a++;
}
// property details
$db->query("select * from availability where pid = $pid");
$b = 0;
while($bookings=$db->fetchassoc()) {
$propertyBookings[$b]['aid'] = $bookings['tid'];
$propertyBookings[$b]['pid'] = $bookings['pid'];
$propertyBookings[$b]['from'] = $bookings['by'];
$propertyBookings[$b]['to'] = $bookings['body'];
$propertyBookings[$b]['cid'] = $bookings['cid'];
$b++;
}
$db->close();
$propertyArray = array(
'details' => $propertyDetails,
'images' => $propertyImages,
'features'=> $propertyFeatures,
'reviews' => $propertyReviews,
'bookings'=> $propertyBookings
);
return $propertyArray;
}
My expectation would be to see a nice fat array with loads of properties in ( by that i mean buildings) and loads of sub arrays and a bunch more data in there too… what i get is this measly offering…
array(1) {
[0]=> array(5) {
["details"]=> array(1) {
[0]=> array(9) {
["pid"]=> string(1) "1"
["name"]=> string(12) "Casa De Paul"
["description"]=> string(134) "a small, dark, damp building in the middle of nowhere with no shops, or roads, lots of spiders and a spikey bed for almost one person."
["rooms"]=> string(1) "2"
["baths"]=> string(1) "1"
["sleeps"]=> string(1) "4"
["rental"]=> string(4) "0.00"
["sale"]=> string(4) "0.00"
["forsale"]=> string(1) "0" } }
["images"]=> array(0) { }
["features"]=> array(0) { }
["reviews"]=> array(0) { }
["bookings"]=> array(0) { } } }
just for info: when i query the DB in phpMyAdmin using “Select * from properties” i get all the results i would have expected ….
theres something ive missed – or over baked, and i cant see it and my head is about to go POP !
Thanks in advance peoples of SO 😀
It seems that after you call
mysql_fetch_assoc($this->result)on the first details query and looped through the resource result already so the pointer is no longer pointing at any rows.If you decide to loop through the same resource result again, the function will always return false because it will assume there are no more rows.
Try to reset your pointer after every loops:
Then continue your next query and fetch data loops.