简体   繁体   中英

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 ?

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".

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:

$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]

Execute this trough PHP and echo the results somewhere

(* in the Select works too, as long as you just only echo the value_name property)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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