用php检索数据库

时间:2013-06-24 21:01:04

标签: php sql database join

我正在创建一个页面,从注册会员的论坛中获取联系方式。我试图检索自定义字段时遇到了一些问题。对于大学和航班,使用下拉菜单,以便用户可以从特定列表中进行选择。然而论坛使用3个不同的表来存储数据。 这只是表格的一小部分

db mock up http://www.emuas.co.uk/images/db_mock_up.png

目前我获得了uni查询和航班查询的航班,但结果与user_id无关。不知道如何改变它。

$user = "SELECT * 
  FROM profile_fields_data 
  JOIN users 
    ON profile_fields_data.user_id = users.user_id 
  ORDER BY users.username";

$uni = "SELECT * 
  FROM profile_fields_data
  JOIN users 
    ON profile_fields_data.user_id = users.user_id
  JOIN profile_fields_lang 
    ON profile_fields_data.pf_university = profile_fields_lang.option_id
  WHERE field_id =4
  ORDER BY users.username";

$flight = "SELECT * 
  FROM profile_fields_data
  JOIN users 
    ON profile_fields_data.user_id = users.user_id
  JOIN profile_fields_lang 
    ON profile_fields_data.pf_flight = profile_fields_lang.option_id
  WHERE field_id =3
  ORDER BY users.username";

$userdata = $db->sql_query($user);
$unidata = $db->sql_query($uni);
$flightdata = $db->sql_query($flight);
while($data = $db->sql_fetchrow($userdata))
{
  $datauni = $db->sql_fetchrow($unidata);
  $dataflight = $db->sql_fetchrow($flightdata);
  echo "<tr>
  <td> <a href='http://emuas.co.uk/forum/memberlist.php?mode=viewprofile&u=" .    $data['user_id'] . "'>" . $data['username'] . "</a></td>
  <td> <a href='mailto:" . $data['user_email'] . "'>" . $data['user_email'] . "</a>  </td>
  <td>" . $data['pf_contact_number'] . "</td>
  <td>" . $data['user_birthday'] . "</td>
  <td>" . $data['pf_service_number'] . "</td>
  <td>" . $datauni['lang_value'] . "</td>
  <td>" . $dataflight['lang_value'] . "</td>
  <td>" . $data['pf_secondary_duty'] . "</td>
  </tr>";

非常感谢

1 个答案:

答案 0 :(得分:0)

发现问题。当论坛获得option_id的数据时,它将值加1。我已经改变它以考虑到这一点