我已经使用 PHP 好几年了,OOP、类等对我来说并不是一个陌生的概念,但出于某种原因,有一些事情(没有)发生,我可以理解,现在是凌晨 1 点,并且它可能与它有关,但请......有人让我摆脱了午夜的痛苦!
我有一个 PHP DB 类和一个填充的 DB,还有几个函数在这里发挥作用。我知道数据库中有超过 50 行应该返回,但我一直只得到一个!
这是我的数据库连接课程..
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));
}
}
好的,这是我的实际查询功能...
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;
}
我的期望是看到一个漂亮的胖数组,其中包含大量属性(我的意思是建筑物)和大量子数组以及更多数据......我得到的是这个微不足道的产品......
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) { } } }
仅供参考:当我使用“从属性中选择 *”查询 phpMyAdmin 中的数据库时,我得到了我所期望的所有结果......
我错过了一些东西 - 或者烤过头了,我看不到它,我的头快要流行了!
在此先感谢 SO 的人们:D