2017-03-20 16 views
1

今私はいくつかの情報を含むSQLデータベースを作成しました。PHPを使用してSQLデータベースから情報を取得

私は現在、次の列を持つ3つのテーブルを持っている:

(1)テーブル名=上院議員。 columns = Dnumber、Name (2)テーブル名=委員会;列= ID、委員会 (3)テーブル名= memberCommittee;列= ID、DNUMBER

私のselect文:

をmとして、 cとmemberCommitteeをSとして上院議員、委員会FROM上院議員としてDISTINCT s.nameをSELECT WHERE s.Dnumber = m.Dnumberとc.ID = m.ID およびc.ID = "1001"。

これは、3つのテーブルを使用して情報を取得し、ID「1001」の「名前」を表示することを目的としています。私の選択した声明は、1001というIDを持つ委員会のメンバーを表示するように機能しますが、情報を取得する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($ CONN、「Sなどの上院議員FROM上院議員としてDISTINCT s.nameを選択して、Cのような委員会、memberCommittee mとWHERE s.Dnumber = m.Dnumberこの結果を、$ result = mysqli_query($ conn、 "SELECT DISTINCT s.name"を上院議員として議会議員として、委員会を次のように任命します:c.ID = m.IDとc.ID = "1001" c、memberCommitteeはmとなります。s.Dnumber = m.Dnumberおよびc.ID = m.IDおよびc。あなたの場所の一重引用符 –

+0

@GuillermoAndresFuentesMoralはそれを変更しました... "While"ステートメントで変数が何を意味するかわからない –

+0

あなたのprint_r変数 '$ row'?結果を表示しますか? –

答えて

0

あなたは、単一引用符内

$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'");  

を1001ラップすべきであり、あなたが行が

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

を引き起こすエコーすべきである(間違った二重引用符シーケンスを持っていますあなたがtの整数としてc.IDを持っていないことを確かめてくださいあなたが)あなたがsintaxに参加する明示的な伊勢なければならない単一引用符

$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;");  

と(単に提案)withou 1001を使用

$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

私は何の結果も得ていません。 –

0

mysqli_query()すべきである彼の場合は、失敗時には偽のmysqli_resultオブジェクトを()を返します。このオブジェクトには:: fetch_assoc()(またはfetch_assoc()、手続き型)というメソッドがあり、引数をとらず配列を返します。 whileは、すべての行が一度に1つずつ$ row変数に返されるまで反復処理に使用されます。 $ rowがfetch_assoc()からfalseになると、ループは停止します。 は、マニュアルを確認してください: http://php.net/manual/en/mysqli-result.fetch-assoc.php

関連する問題