如何从第一个查询中获取字段的值并在第二个查询中使用它

时间:2013-06-03 05:37:12

标签: php mysql

我有来自两个表的两个查询,一个是属性,第二个是属性的图像。

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
      }
  }
  }
  }

?>      

1 个答案:

答案 0 :(得分:0)

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

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

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