2017-03-20 105 views
1

現在我已經創建了一個SQL數據庫,其中包含一些信息。使用PHP從SQL數據庫獲取信息

我現在有包含以下列三個表:

(1)表名=參議員;列= Dnumber,名稱 (2)表名=委員會;列= ID,委員會 (3)表名= memberCommittee;列= ID,Dnumber

我的選擇語句:

SELECT DISTINCT s.name參議員FROM參議員爲s,委員會如 C,memberCommittee爲m WHERE s.Dnumber = m.Dnumber和c.ID = m.ID 和c.ID =「1001」;

這樣做的目標是使用三個表獲取信息並顯示ID爲「1001」中的「名稱」。我的select語句的工作原理是向委員會中的人員顯示ID爲1001的人員,但我不太瞭解獲取信息的PHP部分。

這一部分:

*

$result = mysqli_query($conn, "SELECT DISTINCT s.name as Senator 
    FROM senators as s, committees as c, memberCommittee as m 
    WHERE s.Dnumber =m.Dnumber and c.ID = m.ID and c.ID = "1001"); 

    while ($row = mysqli_fetch_assoc($result)){ 
      unset($id, $Senator);   
      //$id = $row['Features']; 
      $Senator = $row['Senator']; 
      echo '<option value="'.$Senator.'">'.$Senator.'</option>';  

    } 

*

有人能幫助我嗎?

<!DOCTYPE html> 
 
<html> 
 
\t <head> 
 
\t \t <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
 
\t \t \t <script language="javascript" type="text/javascript"> 
 
\t \t \t function dynamicdropdown(listindex) { 
 
\t \t \t document.getElementById('senator').className = listindex; 
 
\t \t \t </script> 
 
\t \t \t 
 
\t \t \t <style> 
 
\t \t \t optgroup { 
 
\t \t \t display: none; 
 
\t \t \t } 
 

 
\t \t \t select.Agriculture 
 
\t \t \t optgroup.Agriculture 
 
\t \t \t { 
 
\t \t \t display: block; 
 
\t \t \t } 
 
\t \t \t 
 
\t \t \t div#header{ 
 
\t \t \t padding: 1px; 
 
\t \t \t color: yellow; 
 
\t \t \t padding-left: 9px; 
 
\t \t \t background-color: #000080; 
 
\t \t \t } 
 
\t \t \t 
 
\t \t \t .category_div{ 
 
\t \t \t padding: 3px; 
 
\t \t \t } 
 
\t \t \t 
 
\t \t \t .sub_category_div{ 
 
\t \t \t padding: 3px; 
 
\t \t \t } 
 
\t \t \t 
 
\t \t \t .microphone{ 
 
\t \t \t padding: 3px; 
 
\t \t \t } 
 
\t \t \t 
 
\t \t \t .body{ 
 
\t \t \t padding-right: 5px; 
 
\t \t \t } 
 
\t \t \t </style> 
 
\t </head> 
 
\t 
 
<body> 
 

 
\t <div class="header" id="header"> 
 
\t <h1>Indiana State Senate IT</h1> 
 
\t </div> 
 

 
\t <div class="room130"> 
 
\t <h3>Room 130</h3> 
 
\t <form target="Room 130" action = "room130.php" method="POST"> 
 
\t \t <div class="category_div" id="category_div">Committee: 
 
\t \t \t <select id="committee" name="committee" onchange="javascript: dynamicdropdown(this.options[this.selectedIndex].value);"> 
 
\t \t \t \t <option value="">Select Committee</option> 
 
\t \t \t \t <option value="Agriculture">AGRICULTURE</option> 
 
\t \t \t </select> 
 
\t \t </div> 
 
\t \t 
 
\t \t <div class="sub_category_div" id="sub_category_div"> 
 
\t \t Individual: 
 
\t \t \t <select name="senator" id="senator"> 
 
\t \t \t \t <option value="">Select individual</option> 
 
\t \t \t \t \t <optgroup class="Agriculture"> 
 
\t \t \t \t \t \t <?php 
 
\t \t \t \t \t \t \t $conn = mysqli_connect("db.soic.indiana.edu", "i308f16_team43", "my+sql=i308f16_team43", "i308f16_team43"); 
 

 
\t \t \t \t \t \t \t if(!$conn){ 
 
\t \t \t \t \t \t \t \t \t die("Connection Failed".myslqi_connect_error()); 
 
\t \t \t \t \t \t \t } 
 
\t \t \t \t \t \t \t \t \t $result = mysqli_query($conn, "SELECT DISTINCT s.name as Senator FROM senators as s, committees as c, memberCommittee as m WHERE s.Dnumber = m.Dnumber and c.ID = m.ID and c.ID = "1001"); 
 
\t \t \t \t \t \t \t \t \t while ($row = mysqli_fetch_assoc($result)){ 
 
\t \t \t \t \t \t \t \t \t \t \t unset($id, $Senator); 
 
\t \t \t \t \t \t \t \t \t \t \t //$id = $row['Features']; 
 
\t \t \t \t \t \t \t \t \t \t \t $Senator = $row['Senator']; 
 
\t \t \t \t \t \t \t \t \t \t \t echo '<option value="'.$Senator.'">'.$Senator.'</option>'; 
 
\t \t \t \t \t \t \t \t \t } 
 
\t \t \t \t \t \t \t ?> \t \t \t \t \t \t \t \t 
 
\t \t \t \t \t </optgroup> 
 
\t \t \t \t </option> 
 
\t \t \t </select> 
 
\t \t </div> 
 
</body> 
 
</html>

+0

檢查該行'$結果= mysqli_query($康恩,「SELECT DISTINCT s.name參議員FROM參議員爲s,委員會爲c,memberCommittee爲m WHERE s.Dnumber = m.Dnumber和c.ID = m.ID和c.ID =「1001」);'缺乏 - >「使用這個'$ result = mysqli_query($ conn,」SELECT DISTINCT s.name作爲Senator FROM參議員作爲s委員會c,memberCommittee as m WHERE s.Dnumber = m.Dnumber and c.ID = m.ID and c。ID ='1001'「)'您的單引號 –

+0

@GuillermoAndresFuentesMoral更改了它......不確定變量在」While「語句中的含義 –

+0

您的print_r變量'$ row'?顯示結果? –

回答

0

你有一個錯誤的雙引號序列(你應該換行1001單引號

$result = mysqli_query($conn, "SELECT DISTINCT s.name as Senator 
    FROM senators as s, committees as c, memberCommittee as m 
    WHERE s.Dnumber =m.Dnumber and c.ID = m.ID and c.ID = '1001'");  

內,你應該呼應行的結果

echo '<option value="'.$row['Senator'].'">'. $row['Senator'] .'</option>'; 

並確保你在t中沒有c.ID作爲整數他的情況下,你應該使用1001 withou單引號

$result = mysqli_query($conn, "SELECT DISTINCT 
          s.name as Senator 
          FROM senators as s 
          , committees as c 
          , memberCommittee as m 
          WHERE s.Dnumber =m.Dnumber 
          and c.ID = m.ID 
          and c.ID = 1001;");  

和(只是一個建議),你應該ISE明確加入sintax)

$result = mysqli_query($conn, "SELECT DISTINCT 
           s.name as Senator 
           FROM senators as s 
           INNER JOIN memberCommittee as m on s.Dnumber =m.Dnumber 
           INNER JOIN committees as c on and c.ID = m.ID 
           WHERE c.ID = 1001"); 
+0

我做了正確的修復,並且沒有完成我假設我的變量是不正確的?如果它是可以幫助我的嗎? –

+0

瞭解您在代碼中的位置..得到一個錯誤的值的選項列表,或者你沒有選擇的價值?換句話說,你的選擇給你一些結果嗎? – scaisEdge

+0

我沒有得到任何結果。它必須在「while部分」中正確地獲取變量。 –

0

mysqli_query()失敗則返回mysqli_result對象(假)。該對象有一個名爲:: fetch_assoc()(或fetch_assoc(),過程式)的方法,它不接受任何參數並返回一個數組。用於迭代直到所有行返回(一次一個)到$ row變量。當$ row從fetch_assoc()變爲false時,循環停止。 檢查手動: http://php.net/manual/en/mysqli-result.fetch-assoc.php