duanqin9507 2016-01-06 13:23
浏览 193

PHP for循环sql结果

I'm currently working on a project for school. I have 2 SQL tables and one table to connect the two. Tabel one contains people, table 2 contains card. The connect table contains a primary key, and 2 values. On value is the primary key of a person, the other is the primary key of a car. This table connects the people to the cars. With this I can make a table in PHP in which i can see who has wich car.

This works fine, however every person that has multiple cars is shown more than once. So, for example: person one had both car number one and number two. The table now shows the name of person on 2 times and behind his name the car. I would like it to show a table with the names of the persons just once, and then one or more cars behind that.

Below is an example of the way I have it set up now:

$query = "SELECT firstname, lastname, car FROM people, cars, connecttabel         
WHERE connecttabel.personnumber = people.personnumer
AND connecttabel.carnumber = cars.carnumber";
$result = mysqli_query($database, $query)
or die ("error");
echo "<table>";
while($record=mysqli_fetch_array($result))
{$firstname = $record['firstname'];

and so on. With the variables I fill in a table.

Does anyone have any idea how to help me out here? I think it should be done with a for loop but i've been trying for the last 3 hours and I can't get it done.

  • 写回答

2条回答 默认 最新

  • douyoupingji7238 2016-01-06 13:27
    关注
    $query = "SELECT firstname, lastname, car FROM people, cars, connecttabel         
    WHERE connecttabel.personnumber = people.personnumer
    AND connecttabel.carnumber = cars.carnumber Group by people.id";
    $result = mysqli_query($database, $query)
    or die ("error");
    echo "<table>";
    while($record=mysqli_fetch_array($result))
    {$firstname = $record['firstname'];
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog