私は数年前からPHPを使用しており、OOPやクラスなどは私にとって異質な概念ではありませんが、何らかの理由で、頭を動かすことができる(今は午前1時)ということが起こっています。それはそれと関係があるかもしれませんが、お願いします..誰かが私の真夜中の惨めさから私を追い出しました!!
私はPHPDBクラスと、データが入力されたDBを持っており、ここでいくつかの関数を使用しています。データベースに返される必要のある行が50行以上あることはわかっていますが、取得し続けるのは1つだけです。
これがDB接続の私のクラスです。
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でDBにクエリを実行すると、期待したすべての結果が得られます...。
何か見落としているものがあります-または焼き過ぎです、そして私はそれを見ることができず、私の頭はPOPになりそうです!
SO:Dの人々に事前に感謝します