gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:39:01 24 4
gpt4 key购买 nike

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

PropertyID字段在两个表中都有,PK--->FK关系。

现在我的问题是如何使用我在

中收到的 propertyID 的值

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

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

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

这是我的代码:

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

?>

最佳答案

正如 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

关于php - 如何从第一个查询中获取字段的值并在第二个查询中使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16890566/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com