簡體   English   中英

如何從數據庫中的三個表中同時獲取數據

[英]how can i fetch data from three tables in database all together

我想從我的數據庫中的三個表中獲取數據並將它們顯示在網頁上。 發現了很多類似的問題,但沒有一個對我有幫助。 這里我的 php 代碼是

<?php

session_start();
include('database.php');

$sql = "SELECT * FROM class8,class9,class10 WHERE teacher_id = :id";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':id', $_SESSION['teacher_id']);

$result = $stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_OBJ)) {

    var_dump($row);
}

?>

從這段代碼中我試圖獲取但它給出了這樣的錯誤


Fatal error: Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'teacher_id' in where clause is ambiguous in C:\xampp\htdocs\kridha\prospectcorse.php:10 Stack trace: #0 C:\xampp\htdocs\kridha\prospectcorse.php(10): PDOStatement->execute() #1 {main} thrown in C:\xampp\htdocs\kridha\prospectcorse.php on line 10

怎么做..??/

您需要連接表,並在引用多個表中存在的列時指定表名。

SELECT *
FROM class8
JOIN class9 ON class8.teacher_id = class9.teacher_id
JOIN class10 ON class8.teacher_id = class10.teacher_id
WHERE class8.teacher_id = :id

暫無
暫無

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

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