简体   繁体   中英

how to get a value of a field from first query and use it in second query

I have two queries from two tables one is properties and second is images of properties.

The PropertyID field is available in both tables which have relationship of PK--->FK.

NOW my question is here how can I use the value of propertyID which I have received in

the first query and use it in the second one for retrieving the images of each property.

I 've wrote some code, but I receive this error message:

Notice: Trying to get property of non-object in C:\\xampp\\htdocs\\Scale-Property\\spd\\index.php on line .......

Here is my code:

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

?>      

Like Burhan Khalid suggested, you need to join the tables like this:

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

Where the on part links the PK and FK of the tables. Check the mysql syntax here: http://dev.mysql.com/doc/refman/5.0/en/join.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM