2017-02-28 84 views
0

我想從兩個表細節我database.I使用下面的代碼獲取使用兩個表

$sqlall="SELECT 
     student_detail.reg_no 
     , student_detail.full_Name 
     , student_detail.year_of_study 
     , student_detail.faculty 
     , student_detail.course 
     ,student_hostel.reg_no 
     ,student_hostel.room_no 
     ,student_hostel.hostel_id 
    FROM student_detail,student_hostel 
    WHERE student_detail.reg_no = student_hostel.reg_no; "; 


//This is print part 
$result = $conn->query($sqlall); 

if ($result->num_rows > 0) { 
    // output data of each row 
    //create table 

    echo "<table class=\"table table-hover\"> 
     <tr> 
     <th>Reg. No.</th><th>Hostel id</th><th>Room No</th></tr>"; 
    while($row = $result->fetch_assoc()) { 

    echo"<tr id=\"".$row["student_detail.reg_no"]."\" onclick=\"Redirect(this.id)\"><td>".$row["student_detail.reg_no"]."</td><td>".$row["student_hostel.room_no"]."</td><td>".$row["student_hostel.room_no"]."</td></tr>"; 
    } 
    echo "</table>"; 
    } 
    else 
    { 
     echo "<table class=\"table table-hover\"> 
      <tr> 
      <th>Reg. No.</th><th>Hostel id</th><th>Room No</th></tr>; 
      <span id></span> 
      </table>"; 
    } 

一個查詢會得到數據後,然後出現錯誤

公告:未定義指數:student_detail.reg_no在C:\ XAMPP \ htdocs中\ HMS \ HMS \ LIB \ HOS_current.php上線47

我怎樣才能解決呢?

+0

你將有權發佈比則 –

+0

請只是呼應了行不表名'回聲$行更多[ 'reg_no'];' –

+0

當你運行查詢和你如何顯示它時,用代碼編輯問題 –

回答

0

試試這個:

$sqlall="SELECT student_detail.reg_no, student_detail.full_Name, student_detail.year_of_study, student_detail.faculty, student_detail.course,student_hostel.reg_no,student_hostel.room_no,student_hostel.hostel_id FROM student_detail INNER JOIN student_hostel ON student_detail.reg_no=student_hostel.reg_no; "; 
+1

**試試這個**並不能真正幫助OP瞭解他做錯了什麼 –

1

你必須在一開始就錯了單引號與一backtics

你必須在一開始的student_detail錯單引號student_detail.reg_no改變它。 reg_no改變它與backtics
廣告的bakctics應該包裝表和列distincly 和你有backtics也產生了拖車表

$sqlall="SELECT 
      `student_detail`.`reg_no` 
      , `student_detail`.`full_Name` 
      , `student_detail`.`year_of_study` 
      , `student_detail`.`faculty` 
      , `student_detail`.`course` 
      ,`student_hostel`.`reg_no` 
      ,`student_hostel`.`room_no` 
      ,`student_hostel`.`hostel_id` 
     FROM student_detail,student_hostel 
     WHERE `student_detail`.`reg_no` = `student_hostel`.`reg_no`; "; 

,或者由於事實,你有沒有保留字或空間,不要在這種情況下使用backics

$sqlall="SELECT 
      student_detail.reg_no 
      , student_detail.full_Name 
      , student_detail.year_of_study 
      , student_detail.faculty 
      , student_detail.course 
      ,student_hostel.reg_no 
      ,student_hostel.room_no 
      ,student_hostel.hostel_id 
     FROM student_detail,student_hostel 
     WHERE student_detail.reg_no = student_hostel.reg_no; "; 

第二個錯誤你是在FATC while循環是指查詢應返回行..所以誤差可能與列名...使用別名比如嘗試(試驗):

SELECT 
      student_detail.reg_no as no 
      ..... 

,並指給你列while循環這樣

echo"<tr id=\"".$row['no']."\" onclick=\"Redirect(this.id)\"><td>".$row['no']. 
     "</td><td></td><td></td></tr>"; 
+0

@JohnHC。我已經alraedy看到..我有更新的答案..無論如何非常感謝您的建議 – scaisEdge

+0

Thnx BT它沒有工作它 –

+0

顯示確切的錯誤信息請 – scaisEdge

1

嘗試:

$sqlall= " 
SELECT student_detail.reg_no, 
     student_detail.full_Name, 
     student_detail.year_of_study, 
     student_detail.faculty, 
     student_detail.course, 
     student_hostel.reg_no, 
     student_hostel.room_no, 
     student_hostel.hostel_id 
FROM student_detail 
INNER JOIN student_hostel 
ON student_detail.reg_no = student_hostel.reg_no;"; 

見ANSI JOIN語法?此外,不良反引號和apostophes用原代碼

如果你真的必須使用反引號,用身邊的每個實體:

`student_detail`.`reg_no` 
2

我會建議你使用SQL連接,SQL連接使用合併來自兩個或更多表格的行。

這是如何實現你想要什麼用加入

$sqlall = "SELECT DISTINCT student_detail.reg_no, student_detail.full_Name, student_detail.year_of_study, student_detail.faculty, student_detail.course,student_hostel.reg_no,student_hostel.room_no,student_hostel.hostel_id FROM student_detail inner join student_hostel on student_hostel.reg_no 
    = student_detail.reg_no"; 
$result = $conn->query($sqlall); 

if ($result->num_rows > 0) { 
    // output data of each row 
    //create table 

    echo "<table class=\"table table-hover\"> 
     <tr> 
     <th>Reg. No.</th><th>Hostel id</th><th>Room No</th></tr>"; 
    while($row = $result->fetch_assoc()) { 

    echo"<tr id=\"".$row["reg_no"]."\" onclick=\"Redirect(this.id)\"><td>".$row["reg_no"]."</td><td>".$row["room_no"]."</td><td>".$row["room_no"]."</td></tr>"; 
    } 
    echo "</table>"; 
    } 
    else 
    { 
     echo "<table class=\"table table-hover\"> 
      <tr> 
      <th>Reg. No.</th><th>Hostel id</th><th>Room No</th></tr>; 
      <span id></span> 
      </table>"; 
    } 
?> 

在上面我已經使用inner join

的INNER JOIN關鍵字只要選擇兩個表中的所有行作爲 有是兩個表中的列之間的匹配。

的基本語法爲這個連接是:

SELECT column_name(s) 
FROM table1 
INNER JOIN table2 
ON table1.column_name=table2.column_name; 

SELECT column_name(s) 
FROM table1 
JOIN table2 
ON table1.column_name=table2.column_name; 

NB:INNER JOIN是一樣的JOIN。

注意:當你打印出結果,不只是echo $row['tableName.ColumnName'];出來呼應$row['ColumnName'];