简体   繁体   English

如何从数据库中的三个表中同时获取数据

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

i want to fetch the data from three tables from my database all together and show them on a webpage.我想从我的数据库中的三个表中获取数据并将它们显示在网页上。 there have found many similar questions like this but none of them helped me.发现了很多类似的问题,但没有一个对我有帮助。 here my php code is这里我的 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);
}

?>

from this code i have tried to fetch but it gives error like this从这段代码中我试图获取但它给出了这样的错误


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

how to do that..??/怎么做..??/

you need to join the tables, and specify the table names when referring to columns that exist in multiple tables.您需要连接表,并在引用多个表中存在的列时指定表名。

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