Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-MYSQL类&;SQL问题_Php_Mysql - Fatal编程技术网

PHP-MYSQL类&;SQL问题

PHP-MYSQL类&;SQL问题,php,mysql,Php,Mysql,我已经使用PHP好几年了,OOP、类等对我来说并不是一个陌生的概念,但出于某种原因,有一些事情(没有)发生了,我只是想弄清楚,现在是凌晨1点,它可能与之有关,但请。。有人让我摆脱了午夜的痛苦 我有一个PHP DB类,一个填充的DB,还有几个函数。。我知道数据库中有50多行应该返回,但我一直只得到一行 这是我的DB连接课程 Class DbConnect { var $host = 'localhost'; var $user = 'user'; var $password = 'pass

我已经使用PHP好几年了,OOP、类等对我来说并不是一个陌生的概念,但出于某种原因,有一些事情(没有)发生了,我只是想弄清楚,现在是凌晨1点,它可能与之有关,但请。。有人让我摆脱了午夜的痛苦

我有一个PHP DB类,一个填充的DB,还有几个函数。。我知道数据库中有50多行应该返回,但我一直只得到一行

这是我的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中使用“Select*from properties”查询数据库时,我得到了我预期的所有结果

有些东西我错过了,或者烤过头了,我看不见,我的头都快爆了


提前感谢SO:D的人们

在第一次详细信息查询中调用
mysql\u fetch\u assoc($this->result)
后,指针似乎已经在资源结果中循环,因此指针不再指向任何行

如果决定再次循环相同的资源结果,函数将始终返回false,因为它将假定不再有行

尝试在每次循环后重置指针:

// Reset Pointer.
mysql_data_seek( $this->result );

$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++;
}

mysql_data_seek( $this->result );

// etc....

然后继续您的下一个查询并获取数据循环。

如果您正在寻求改进,我的建议是:a)摆脱这种旧的PHP 4 OOP风格,b)摆脱mysql_*函数,它们已弃用,将从PHP中删除,c)您没有暴露转义方法,所以我很确定你所有的sql语句都有很多sql注入漏洞……我正在重建一个现有的站点,而付出的代价却很少。。尽管如此,这些都是有效的观点,我们将提出建议。关于初始点有什么想法吗?看起来好像你的类包装了mysq*接口。看看条令或推进,这些框架有一个很好(可能更安全)的界面。可能会有一个学习曲线,但它会有回报。如果
SELECT*FROM properties
对您很有效,为什么不在
getAllProperties()
函数中返回它的结果呢?我编辑了getPropertyDetails()函数来实现这一点。。。我想问题不在这里,而是在于getAllProperties()函数:我将其简化为var_dump从mysql返回的数组内容。。。我仍然得到一个结果。。数组(1){[“pid”]=>string(1)“1”}数组(0){}$propertyArray=array()$db=新的DbConnect()$db->open()或die($db->error())$db->query(“从属性中选择pid”)$详细信息=$db->fetchassoc()$db->close();变量转储($details);}@迪诺编辑了我的答案。关于您的评论,因为您只调用一次
$details=$db->fetchassoc()。这意味着您只能从结果中获取一行,您需要使用
while()
,就像在
getPropertyDetails()
上一样!!!就这样。。我的大脑已经平静了,谢谢你。。。现在我要去睡觉,梦见我的大数组,以及如何最好地处理它:D@SubRed