为什么我的表中只显示一个结果?

时间:2015-04-27 11:49:27

标签: php mysql mysqli

我正在使用php my mysqli当用户从数据库中选择一个house id并显示使用select查询在表中显示结果时。因为它只显示一行,即使它有两行使用相同的数字,任何想法如何解决这个问题。

<form action="back.php" method="post"> 

<p>Chose a house ID</p>

<select name="HouseID"> 


  <?php

include "connect.php";

$query = "SELECT distinct `HouseID` from purchase";
$result = mysqli_query($con, $query) or die("Invalid query");  
while($rows = mysqli_fetch_array($result))

{ 

      echo "<option value=\"" . $rows[0] . "\">" . $rows[0] ."</option>"; 
} 

echo "</select>"; 
         

mysqli_close($con); 
?> 

<p>Choose the ORDER BY attribute</p>

<select name ="list">
 
  <option value="PurchaseID">PurchaseID</option>
  <option value="HouseID">HouseID</option>
  <option value="DatePurchased">DatePurchased</option>
  <option value="DatePurchased">DatePurchased</option>
  <option value="AskingFor">AskingFor</option>
  <option value="SoldFor">SoldFor</option>
  <option value="AgentID">AgentID</option>
  <option value="CustomerID">CustomerID</option>
</select>

<p>Choose ASC or DESC attribute order</p>

ASC<input type="radio" name="order" value="ASC" checked>

DESC<input type="radio" name="order" value="DESC" checked>

<br>
<br>
<input type="submit" value="Submit Value">
</form></body></html> 

<?php

$HouseID = $_POST["HouseID"];
$list = $_POST["list"];
$order = $_POST["order"];

include "connect.php";

$query = "SELECT `HouseID`,`AskingPrice`,`SoldFor`,`DatePurchased` FROM purchase where `HouseID` = $HouseID ORDER BY  ";
$result = mysqli_query($con, $query) or die($mysqli->connect_error); 

echo "<table border='1'><tr><th>House ID</th><th>Asking Price</th><th>Sold For</th><th>Date Purchased</th></tr>";  
$row = mysqli_fetch_array($result);


echo "<tr><td>" . $row[0] .  "</td><td>" .  $row[1] .  "</td><td>" .  $row[2] .  "</td><td>" .  $row[3] .  "</td></tr>";
echo "</table>"; 
mysqli_free_result($result);
mysqli_close($conn); 

?>

3 个答案:

答案 0 :(得分:1)

你需要循环播放数组。

否则,它只显示最后一个元素。

function printObj(obj, hierarchyLevel) 
  if (hierarchyLevel == nil) then
    hierarchyLevel = 0
  elseif (hierarchyLevel == 4) then
    return 0
  end

  local whitespace = ""
  for i=0,hierarchyLevel,1 do
    whitespace = whitespace .. "-"
  end
  io.write(whitespace)

  print(obj)
  if (type(obj) == "table") then
    for k,v in pairs(obj) do
      io.write(whitespace .. "-")
      if (type(v) == "table") then
        printObj(v, hierarchyLevel+1)
      else
        print(v)
      end           
    end
  else
    print(obj)
  end
end

答案 1 :(得分:0)

我认为你的意思是选项没有完全显示

好吧,只需添加一个循环:

替换它:

echo "<table border='1'><tr><th>House ID</th><th>Asking Price</th><th>Sold For</th><th>Date Purchased</th></tr>";  
$row = mysqli_fetch_array($result);


echo "<tr><td>" . $row[0] .  "</td><td>" .  $row[1] .  "</td><td>" .  $row[2] .  "</td><td>" .  $row[3] .  "</td></tr>";
echo "</table>"; 
mysqli_free_result($result);
mysqli_close($conn); 

有了这个:

echo "<table border='1'><tr><th>House ID</th><th>Asking Price</th><th>Sold For</th><th>Date Purchased</th></tr>";  
while($row = mysqli_fetch_array($result))  {

echo "<tr><td>" . $row[0] .  "</td><td>" .  $row[1] .  "</td><td>" .  $row[2] .  "</td><td>" .  $row[3] .  "</td></tr>";
}
echo "</table>"; 
mysqli_free_result($result);
mysqli_close($conn); 

所以,我认为应该有用......

答案 2 :(得分:0)

在你的选择中使用WHERE,此子句只返回1行;

相关问题