简体   繁体   English

显示所有具有相同ID的行

[英]Display all rows with the same id

Here is my table: 这是我的桌子:

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

How can I echo out list of all Value_Name for Place_Id 001 ? 如何回Value_Name Place_Id 001的所有Value_Name列表?

I tried this: 我尝试了这个:

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

and it returned only the last "Value_Name". 并且它仅返回最后一个“ Value_Name”。

Something like this should work 这样的事情应该工作

$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;
 }

So here is the final version thanks to @Alexander Ravikovich and @McNoodles: 因此,这里是最终版本,感谢@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

Assuming table name is jiggles (since you didn't mention it) 假设表名是微妙的(因为您没有提到它)

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

Execute this trough PHP and echo the results somewhere 执行低谷的PHP并在某处回显结果

(* in the Select works too, as long as you just only echo the value_name property) (只要您仅回显value_name属性,“ Select”中的*也会起作用)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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