在PHP中继续MYSQL输出 [英] Continue MYSQL output in PHP

查看:81
本文介绍了在PHP中继续MYSQL输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对数据库有这样的选择:

I have a select on a database, like this:

$result = mysql_query("                 
SELECT dat_eb_registrants.id, dat_eb_registrants.first_name,  
    dat_eb_registrants.last_name, dat_eb_registrants.email, dat_eb_registrants.comment,   
    dat_eb_registrants.amount, dat_eb_registrants.published,   
    dat_eb_registrants.transaction_id, dat_eb_registrants.register_date,   
    dat_eb_field_values.field_value   
FROM dat_eb_registrants LEFT JOIN dat_eb_field_values 
  ON dat_eb_registrants.id=dat_eb_field_values.registrant_id
WHERE dat_eb_field_values.field_id='53' AND `event_id` >= 20 AND `event_id` <= 25
ORDER BY $sort $ascdsc
");

,它在html表中变暗,如下所示: 回声 ID 名 姓 电子邮件 评论 值1 值2 值3 ;

and it gets diplayed in a html table like so: echo " ID First name Last name Email Comment Value1 Value2 Value3 ";

同时($ row = mysql_fetch_row($ result)){

while ($row = mysql_fetch_row($result)) {

echo "<tr>";
echo "<td>" . $row[0] . "</td>";
echo "<td>" . $row[1] . "</td>";
echo "<td>" . $row[2] . "</td>";
echo "<td>" . $row[3] . "</td>";
echo "<td>" . $row[4] . "</td>";

echo "<td>" . $row[9] . "</td>";

echo "<td>" . $row[?] . "</td>";
echo "<td>" . $row[?] . "</td>";
echo "</tr>";
echo "</table>";

}

现在,前5个值可以正确显示,就像第5个一样,但是第6个和第7个值又如何呢?它们不会被MYSQL调用,因为dat_eb_field_values.field_value仅被调用一次,并且被赋值为WHERE dat_eb_field_values.field_id = '53

Now, the first 4 values are being displayed correctly, just like the 5th one, but how about the 6th and the 7th one? they are not being called by MYSQL because dat_eb_field_values.field_value is only called one time, and is assigned the value WHERE dat_eb_field_values.field_id='53

我如何用数据库中的其他值填写表格?

How can i complete the table with the other values in the database?

洛兰特,谢谢你,

推荐答案

如果我理解正确,则您正在尝试为每个人加载多个值.这些值在单独的表中的多行中.

If I understand correctly, you are trying to load multiple values for each person. These values are in multiple rows in a separate table.

您将需要使用GROUP BY子句. JOIN注册人表和值表,就像您现在所做的一样,在WHERE子句之后,是GROUP BY注册人的ID.当然,不要将WHERE限制为某个值的ID,因为那样您将不会获得任何其他值.最后,您可以使用GROUP_CONCAT()将所有值行放入单个结果值中,并以,分隔.您可以将其更改为以</td><td>分隔,以便更轻松地放入表格中.

You'll need to use a GROUP BY clause. JOIN the registrants table with the values table just like you do now, and after the WHERE clause, GROUP BY the registrant's id. And of course, don't restrict the WHERE to a certain value's ID, because then you won't get any other values. Finally, you can use GROUP_CONCAT() to get all the value rows into a single result value, delimited by ,. You can change this to be delimited by </td><td> to make it easier to put into your table.

SELECT registrants.name, GROUP_CONCAT(values.value SEPARATOR '</td><td>')
FROM registrants
LEFT JOIN values USING (registrant_id)
WHERE ...
GROUP BY registrant_id

这篇关于在PHP中继续MYSQL输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆