0

我有一个名为 user_id 的表中的存储值。我有另一个这样的表:

客户项目:

+------------------+-------------+-------------+-------------+
| project_codename | client_id_1 | client_id_2 | client_id_3 |
+------------------+-------------+-------------+-------------+
|       Alpha      |      1      |      2      |      3      |
+------------------+-------------+-------------+-------------+
|       Beta       |      2      |      1      |      0      |
+------------------+-------------+-------------+-------------+
|       Gamma      |      3      |      1      |      0      |
+------------------+-------------+-------------+-------------+

如果 user_id = (client_id_1 or client_id_2 or client_id_3) 那么我想在下拉列表中打印相应的 project_codenames。这是我拥有的 php,但没有返回正确的结果。我已经推销了我不确定的领域。

PHP(更新并解决):

<?php

  $con = mysqli_connect("localhost","****","****","db") or die("Connection error: " . mysqli_error($con));

  //stored sample value from another table 
  $user_id = "1";

  //Build query: Not sure if this is correct format
  $query = "SELECT * FROM client_projects WHERE client_id_1 = $user_id OR client_id_2 = $user_id OR client_id_3 = $user_id";
  $result = mysqli_query($con, $query) or die("Query error: " . mysqli_error($con));

  //Drop down list
  echo '<select id="Projects" class="input">';
  echo '<option value="" selected="selected" disabled="disabled">Choose a Project...</option>';

  // Loop through the query results, outputing specific options one by one
  // Not sure of the loop for the options
  while ($row = mysqli_fetch_array($query)) {
    echo '<option value="'.$row['project_codename'].'">'.$row['project_codename'].'</option>';
  }
  echo '</select>';

  mysqli_close($con);
?>
4

1 回答 1

0

您将 $query 直接输入 mysqli_fetch_array(),但此函数需要一个结果资源作为输入。您缺少 mysqli_query() 调用。请参阅手册页中的示例 #2

于 2013-11-13T20:09:54.050 回答