我有以下MySQL查询:
<div class="box" id="settlement1">
<?
$query = "SELECT name, pub, hospital, trade FROM settlements WHERE settlementID = 1";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$settlementName = $row['name'];
$pubExists = $row['pub'];
$hospitalExists = $row['hospital'];
$tradeExists = $row['trade'];
}
?>
<div class="settlement-name"><? echo $settlementName; ?></div>
<div class="settlement-contents">
<?
if ($hospitalExists == '1') { echo "<div class='hospital'>HOSPITAL</div>"; }
if ($pubExists == '1') { echo "<div class='pub'>PUB</div>"; }
if ($tradeExists == '1') { echo "<div class='trade'>TRADE</div>"; }
?>
</div>
</div>
<div class="box" id="settlement2">
<?
$query = "SELECT name, shop, hospital, trade FROM settlements WHERE settlementID = 2";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$settlementName = $row['name'];
$pubExists = $row['pub'];
$hospitalExists = $row['hospital'];
$tradeExists = $row['trade'];
}
?>
<div class="settlement-name"><? echo $settlementName; ?></div>
<div class="settlement-contents">
<?
if ($hospitalExists == '1') { echo "<div class='hospital'>HOSPITAL</div>"; }
if ($pubExists == '1') { echo "<div class='pub'>PUB</div>"; }
if ($tradeExists == '1') { echo "<div class='trade'>TRADE</div>"; }
?>
</div>
</div>
现在,第一个查询工作正常,但第二个返回与第一个查询相同的结果。
所有字段在查询1到查询2中都不同
^ _ ^&lt;(HELP!)
答案 0 :(得分:1)
我不确定这是否是原因,但......
第一个SQL:
$query = "SELECT name, pub, hospital, trade FROM settlements WHERE settlementID = 1";
第二
$query = "SELECT name, shop, hospital, trade FROM settlements WHERE settlementID = 2";
第一个是酒吧,第二个是商店。但是在第二种情况下你没有在PHP中使用商店价值,只有pub(不在这个SQL字段中。
这里可能有SQL错误(数据库中没有字段)或PHP错误(检查结果数组中不存在的字段)。
答案 1 :(得分:0)
是否可以重复使用变量 $ query 或 $ result 导致输出第一个$查询的结果或再次执行相同的查询?