2017-04-19 52 views
-3

我有三個表。三表條件查詢 - php mysql

用戶

________________________________________________ 
|User_ID | User Name | user contact information| 
------------------------------------------------ 

學校

________________________________________________________________ 
|School_ID | School name | school address | school contact info| 
---------------------------------------------------------------- 

圖片

___________________________________________ 
| Picture ID | User_ID | School_ID|file path| 
------------------------------------------- 

每學校ID,我想如下顯示的信息。

學校名稱,學校地址,用戶名,用戶聯繫信息以及所有與學校ID匹配的圖片。

腳本會在php中做什麼。 我已經研究了幾天在這個網站上,並嘗試了很多代碼,但沒有任何工作到目前爲止。

這是我用過的代碼。

<?php 
     $sql="SELECT Schoolname, School_contact_info, email, description, address, website, file, userName FROM school, picture, user WHERE Location_ID=Picture_ID ORDER BY Location_ID desc"; 
     $result_set=mysql_query($sql); 
     while($row=mysql_fetch_array($result_set)) 
     { 
      ?> 
       <div href=<?php $row['Location_ID'] ?>> 
       <td><?php echo $row['Location_ID'] ?></td> 
       <td><?php echo $row['name2'] ?></td> 
       <td><?php echo $row['email'] ?></td> 
       <div><td><?php echo $row['description'] ?></td></div> 
       </div> 

       <div><img src="uploads/<?php echo $row['file'];?>" alt=" " height="75" width="75"></div> 

       <br> 
       <?php 
     } 
    ?> 

我想顯示學校信息,以及所有以school_ID作爲外鍵的圖片。

+0

'並嘗試了很多代碼'...你嘗試了什麼,你能包括嗎?用一個查詢來詢問完整的PHP腳本對於我認爲的單個問題來說太廣泛了。 –

+0

我同意@TimBiegeleisen,這個問題不會得到很好的接受,沒有證明一些自己的努力。 – Shadow

+0

@TimBiegeleisen我用我用過的代碼編輯過這個問題。謝謝。 –

回答

0

查詢到應使用

select school_name,school_address,user_name,user_contact_information 
from user,school,picture where user.user_id=picture_id 
and school.school_id=picture.picture_id 

    <?php 
    $con=mysqli_connect("localhost","my_user","my_password","my_db"); 
    // Check connection 
    if (mysqli_connect_errno()) 
     { 
     echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
     } 

    // Perform queries 
    mysqli_query($con,"select school_name,school_address,user_name,user_contact_information 
    from user,school,picture where user.user_id=picture_id 
    and school.school_id=picture.picture_id"); 
0

在你的表結構你是不是給所有的列詳細那些你正在使用的查詢。
但試試這個: -

SELECT s.Schoolname, s.School_contact_info, s.email, s.description, s.address, s.website, 
p.file, u.userName 
FROM school as s, 
picture as p on s.School_ID=p.School_ID, 
user as u on p.User_ID=u.User_ID 
WHERE s.Location_ID=p.Picture_ID ORDER BY s.Location_ID desc 

我假設像Location_ID,email,website列從校表。