soo我有2张桌子。
train_information
的 user_train_information
当有人在表单中提交某些内容时。它放在train_information
表中,看起来像这样:
现在,当人们登录并从选择器中选择火车时。他的发生在数据库中:
在另一个页面上,我希望用户能够查看他们随时间选择的所有内容。所以我运行了一个查询:SELECT * FROM user_train_information WHERE user_id=user_id;
这会显示表格user_train_information
但是可以显示train_information user_id = user_id吗?因为我希望用户显示他添加的火车。
编辑:
我现在拥有的:
function testingggg() {
$sql = "SELECT *
FROM train_information
INNER JOIN user_train_information
ON train_information.train_id = user_train_information.train_id
WHERE user_train_information.user_id = user_id";
$sth = $this->pdo->prepare($sql);
$sth->bindParam("user_id", $_GET["user_id"], PDO::PARAM_INT);
$sth->execute();
return $sth->fetchAll();
}
我在这里打电话给我:
<?php
$summary = $database->testingggg();
?>
<?php
foreach ($summary as $result){
echo $result['train_name'];
}
?>
我收到错误:
答案 0 :(得分:1)
这就是我在这种情况下使用查询的方式,并且它有效。在你的情况下,某处有歧义。还要确保在$ _GET [&#34; user_id&#34;]中有适当的值。请检查
function testingggg() {
$sql = "SELECT ti.train_name, ti.train_id, uti.user_id
FROM train_information as ti
JOIN user_train_information as uti
ON ti.train_id = uti.train_id
WHERE uti.user_id = :user_id";
$sth = $this->pdo->prepare($sql);
$sth->bindParam(":user_id", $_GET["user_id"], PDO::PARAM_INT);
$sth->execute();
return $sth->fetchAll();
}
答案 1 :(得分:0)
SELECT t.train_id
, t.country_id train_country_id
, t.train_name
, t.number_of_bogies
, t.number_of_axles
, t.wheel_diameter_min
, t.wheel_diameter_max
, u.user_row_id
, u.user_id
, u.country_id user_country_id
FROM user_train_information u
JOIN train_information t
ON t.train_id = u.train_id
WHERE u.user_id = :user_id;
答案 2 :(得分:-1)
你试试这个。
SELECT * FROM train_information INNER JOIN user_train_information
ON train_information.train_id = user_train_information.train_id
WHERE user_train_information.user_id = $user_id;
答案 3 :(得分:-1)
SELECT *
FROM train_information
INNER JOIN user_train_information
ON train_information.train_id = user_train_information.train_id
WHERE user_train_information.user_id = user_id;