簡體   English   中英

如何從PHP SQL中的多個表中獲取數據?

[英]How do I get data from multiple table in PHP SQL?

我正在使用引導程序,PHP和mysql進行學校管理項目。 我剛剛開始使用PHP進行編碼,所以我對此不太熟悉。

我需要從表student_info中選擇所有數據,並通過student_id從表profile_img中僅選擇image_name,如下所示:

學生儀表板


我使用以下SELECT SQL查詢從上表中獲取記錄,並在以下代碼中也包含個人資料圖片:

 <table class="table table-striped table-bordered table-hover" id="dataTables-view"> <thead> <tr> <th>ID</th> <th>Scholar Number</th> <th>Photo</th> <th>Student Name</th> <th>Class</th> <th>Fee Status</th> <th>Action</th> </tr> </thead> <tbody> $sql = mysql_query("SELECT * FROM student_info ORDER BY student_roll_number LIMIT 500"); //Retrive data from table student_info $sql2 = "SELECT * FROM profile_img WHERE student_id = '$id' ORDER BY student_id DESC LIMIT 1"; //Retrive data from table profile_img while($row = mysql_fetch_array($sql)){ $id = $row["student_id"]; $rollnumber = $row["student_roll_number"]; $fname = $row["student_fname"]; $lname = $row["student_lname"]; $class = $row["student_class"]; $image-name = $row["image_name"]; //Retrive data from table two echo ' <tr> <td> '.$id.'</td> <td> '.$rollnumber.'</td> <td class="text-center"> <img src="upload/profile_pic/'.$image-name.'.jpg" style="width:40px;height:30px;"></td> <td> '.$fname.' '.$lname.'</td> <td><i class="icon-group"></i> '.$class.'</td> <td><center>Pending</center></td> </tr>' ;} </tbody> </table> 

我想從student_info表中選擇所有信息,並且只想從表profile_img表中選擇圖像名稱,並顯示在一個單獨的表中,如下面的屏幕截圖所示。

學生儀表板

有人可以幫我嗎? 這是兩個表的屏幕截圖:

student_info

在此處輸入圖片說明

profile_img

在此處輸入圖片說明

當我使用以下代碼時,出現以下錯誤:

  $sql = mysql_query("SELECT student_info.scholar_number, student_info.student_name, student_info.class, student_info.fee_status, profile_img.image_name FROM student_info INNER JOIN profile_img ON profile_img.student_id = student_info.student_id;"); 

在此處輸入圖片說明

查看您的模式是很有用的,但是正如ArtisticPhoenix提到的那樣,您想使用INNER JOIN在公共字段上連接兩個表,例如

SELECT      student_info.scholar_number, 
            student_info.student_name, 
            student_info.class, 
            student_info.fee_status, 
            profile_img.image_name
FROM        student_info
INNER JOIN profile_img 
ON          profile_img.student_id = student_info.student_id;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM