2 while循环在php - mysql select语句中

I have 2 tables in DB:

clients (Show all clients data)

clientsmany (admin could add many phone numbers for each client)

I would like to print all the details about the clients in 1 html table and if any client has more than phone number, all the numbers are printed in the same cell of 'td'

<?php
$result = mysql_query("SELECT * FROM clients");
$result1 = mysql_query("SELECT clientsmany.phone, clients.ID FROM clients INNER JOIN clientsmany ON clients.ID=clientsmany.ClientID");

while($row = mysql_fetch_array($result)){ //Not read!
    while($row1 = mysql_fetch_array($result1)){ //Working correctly and show the list of 'phone' in $row1 for clientsmany.phone
echo "<center><table border='1'>";
echo "<tr><td>".$row['ID']."</td><td>".$row['phone']."<br>".$row1['phone']."</td></tr>";
echo "</table></center>";
}}
?>

Why the 1st while is not working??

The 2nd while only works and print a correct data then it exit automatic!

<?php
echo "<center><table border='1'>";
$result = mysql_query("SELECT * FROM clients");
 while($row = mysql_fetch_array($result)){
 $result1 = mysql_query("SELECT * FROM clientsmany WHERE clientsid=".$row['id']);
 if(mysql_num_rows($result1)>0 ){
 while($row1 = mysql_fetch_array($result1)){
 echo "<tr><td>".$row['ID']."</td><td>".$row['phone']."<br>".$row1['phone']."</td>       </tr>";
}
}else{
 echo "<tr><td>".$row['ID']."</td><td>".$row['phone']."</td></tr>";
}
}
echo "</table></center>";
?>

First, mysql_* functions are depreciated. Try to use mysqli_* functions.

If you want to display data in 1 html table, so why you have started while above table tag?

Try this query..

SELECT clientsmany.phone, clients.* FROM clients, clientsmany WHERE clients.ID=clientsmany.ClientID"

Then use while statement below table tag (No need of 2 different while loop)...

echo "<center><table border='1'>";
while($row1 = mysqli_fetch_array($result1))  {
   // your <tr><td> code
}
echo "</table></center>";

Use GROUP_CONCAT to create a single query and you will be able to a single loop

GROUP_CONCAT will take a column that is repeated and separate each value with a comma (by default it can be changed) and return it into a single value

$query = <<<END
    SELECT 
       clients.*,
       GROUP_CONCAT(clientsmany.phone) as phonenums 
    FROM 
      clients 
    INNER JOIN 
      clientsmany ON clients.ID=clientsmany.ClientID 
    GROUP BY 
      clients.ID 
END;

A query like this would give you all the clients table columns and a column named phonenums which will be a comma separated list of the phone numbers

Now since you only have one query you only need one loop

$db = mysqli_connect(...);

...

//only need to echo out the <table> part once
//so taken out of the while loop
echo "<center><table border='1'>";

$result = mysqli_query($db,$query);
while( ($row = mysqli_fetch_assoc($result)) ) {
   echo <<<END
      <tr>
         <td>{$row['ID']}</td>
         <td>{$row['SomeOtherColumn']}</td>  
         <td>{$row['phonenums']}</td>
      </tr>
END;
}

//Again the </table> only needs done once 
//so taken out of the loop
echo "</table></center>";

Notice the mysli_* functions being used. Mysql api is depreciated, for the most part you can just rename the functions you currently use to mysqli_, but note that some require the $db link as a parameter so make sure to read the php manual on each of the functions so you know how to call them correctly.

Also note that I am using heredoc syntax instead of doing multiple echo calls