2

我从两个表中有两个查询,一个是属性,第二个是属性图像。

PropertyID字段在两个具有 PK--->FK 关系的表中都可用。

现在我的问题是如何使用我收到的propertyID的值

第一个查询并在第二个查询中使用它来检索每个属性的图像。

我已经写了一些代码,但是我收到了这个错误信息:

注意:尝试在线获取 C:\xampp\htdocs\Scale-Property\spd\index.php 中非对象的属性.......

这是我的代码:

<?php
  require_once('../Admin Panel/db.php');
  if(isset($_POST['Province']) && isset($_POST['District']) && isset($_POST['radio']))
  {
      $provincename=$_POST['Province'];
      $districtname=$_POST['District'];
      $propertystatus=$_POST['radio'];
      $query = "SELECT 
    properties.PropertyID,
    properties.PropertyName,
    some other fields......,

    Provinces.ProvinceName,
    districts.DistrictName,
    pds.PDName,

    propertyimages.PropertyID 

   FROM properties, provinces, districts, pds, propertyimages



   WHERE Provinces.ProvinceID=Properties.ProvinceID
   AND   districts.DistrictID=Properties.DistrictID
   AND   pds.PDID=properties.PDID
   AND   ProvinceName='".$provincename."'
   AND   DistrictName='".$districtname."'
   AND   PropertyDealType='".$propertystatus."'  


  ORDER BY properties.PropertyID";

      $queryrun= $connection->query($query); // first query run in here

      while ($row= $queryrun->fetch_assoc()) // in here trying to store the propretyID
      {

      if( $connection->error ) exit( $connection->error );
      $count= $queryrun->num_rows;
       echo 'You Have Got <b>'. $count .'  </b>out of 326 Records';

      while($row = $queryrun->fetch_assoc()) 
      {  
      $imagequery ="SELECT PropertyID, ImagePath, ImageName, FROM properties WHERE PropertyID = '".$row['PropertyID']."'"; 

      // Now i want to use the stored value of propertyID in here for retrieving the 
         Images of related property
      }

       $imagequery_run= $connection->query($imagequery);    
          if($imagequery_run->num_rows > 0)  
              {
                  while ($imagerow = $imagequery_run ->fetch_assoc()) 
                  {

  ?>

  <div class="propertywrapperviewmore">
     <div class="propertysingleimageviewmore">
     <a href="property.php?PropertyID=<?php
      echo htmlentities($imagerow['PropertyID']) ?>&PropertyID=<?php echo htmlentities($propertyrow['PropertyID']) ?>">

                     <img src="<?php echo htmlentities($imagerow['ImagePath'])  ?>" width="227" height="147" alt="<?php echo htmlentities($imagerow['ImageName']) ?>" ></a>
     </div>

     <div class="propertyIDviewmorelablevalue">
               <div class="propertyIDL">Property ID:</div>
               <div class="propertyIDV"><?php echo $row['PropertyID']?></div>
     </div>
     <div class="propertyIDviewmorelablevalue">
               <div class="propertyIDL">Property Name:</div>
               <div class="propertyIDV"><?php echo $row['PropertyName']?></div>
     </div>


  </div>   

  <?php
      }
  }
  }
  }

?>      
4

1 回答 1

0

就像 Burhan Khalid 建议的那样,您需要像这样加入表格:

select [listOfFields]
from properties p
join propertyimages pi 
on p.propertyid = pi.propertyid
where [youWhereClauses]

其中 on 部分链接表的 PK 和 FK。在此处检查 mysql 语法:http: //dev.mysql.com/doc/refman/5.0/en/join.html

于 2013-06-03T12:33:48.547 回答