显示具有相同ID的所有行

时间:2015-04-10 13:56:34

标签: php mysql

这是我的表:

Place_Id | Value_Name| Value
-------------------------------
001      | Name1     | Value1
-------------------------------
001      | Name2     | Value2
-------------------------------
001      | Name3     | Value3
-------------------------------
002      | Name1     | Value4
-------------------------------
002      | Name2     | Value5

如何回显Value_Name 001的所有Place_Id列表?

我试过了:

<?php
$query2 = ("SELECT * FROM table WHERE Place_Id = 001");
if ($statement2 = $db_conn_pdo->prepare($query2))
{
  $statement2->execute();
  while ($row2 = $statement2->fetch(PDO::FETCH_ASSOC)) 
   {
    $output2 = $row2['Value_Name'];
  } 
}
echo $output2;
?>

并且它只返回了最后一个&#34; Value_Name&#34;。

3 个答案:

答案 0 :(得分:0)

这样的事情应该有效

$query = $con ->query
 ("
   SELECT Value_Name FROM [table] WHERE Place_Id = '001'
 ");

$valueName = array();

while($row = $query->fetch_object())  
{
 $valueName[] = $row;
}   

foreach($valueName as $Value)
 {
  echo $Value->Value_Name;
 }

答案 1 :(得分:0)

所以这是最终版本归功于@Alexander Ravikovich和@McNoodles:

$query = $con ->query
 ("
SELECT Value_Name FROM [table] WHERE Place_Id = '001'
 ");

while($row = $query->fetch_object())  
{
 echo $row['Value_Name'];
}   
// the second loop is not needed as @Alexander Ravikovich suggested

答案 2 :(得分:-1)

假设表名是摇摇欲坠的(因为你没有提到它)

SQL查询:SELECT Value_Name FROM jiggles WHERE jiggles.place_id = [the id you want to know]

通过PHP执行此操作并在某处回显结果

(*中的*也适用,只要你只回显value_name属性)