来自 2 个表的数据未显示在同一页面上
Data from 2 tables is not displaying on the same page
我是 PHP 和 mySQL 的新手,我目前正在开发一个个人资料页面,我将在其中显示来自一个名为 table 的已登录用户的个人信息
users
并显示他们将来会从我的网站预订的旅行 table:
booking
我正在使用准备好的语句从用户 table 获取数据,这只会 select 登录用户的数据而没有任何问题,这里是代码:
<?php
include_once 'php/connection.php';
$sess = $_SESSION['email'];
$sql="SELECT firstname, lastname, email, phone, birthday, gender FROM users WHERE email = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $sess);
$stmt->execute();
if ($stmt->error){echo "something has gone wrong";}
$result=$stmt->get_result();
while($row = mysqli_fetch_array ( $result, MYSQLI_ASSOC )) {
?>
我在将使用代码显示数据的部分之后关闭连接:
<h2><?php echo $row["firstname"];?></h2>
<?php
}
$conn->close();
?>
但是,当我尝试从两个 table 中获取数据并将其显示在页面上时,我遇到了问题,我已经进行了研究并尝试了很多选项,但它似乎不起作用,在这里是我试过的:
<?php
include_once 'php/connection.php';
$sess = $_SESSION['email'];
$sql="SELECT firstname, lastname, email, phone, birthday, gender
FROM users, booking
WHERE users.email = booking.email AND email = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $sess);
$stmt->execute();
if ($stmt->error){echo "something has gone wrong";}
$result=$stmt->get_result();
while($row = mysqli_fetch_array ( $result, MYSQLI_ASSOC )) {
?>
通过使用这个脚本,它向我显示了这个错误:
Fatal error: Uncaught Error: Call to a member function bind_param() on boolean in /storage/ssd1/136/16588136/public_html/profile.php:82 Stack trace: #0 {main} thrown in /storage/ssd1/136/16588136/public_html/profile.php on line 82
我一整天都在尝试解决这个问题,但我想不出解决办法。有人可以帮我解决这个问题吗?提前致谢。
你非常接近。由于 SQL 中的错误,您的 ->prepare()
调用失败,因此它 returns 的 $stmt
值为 false 而不是真实语句对象。
当您联接多个 table 时,您需要使用 table 名称限定查询中的列名称。
SELECT u.firstname, u.lastname,
u.email, u.phone, u.birthday, u.gender,
b.something
FROM users u
LEFT JOIN booking b ON u.email = b.email
WHERE u.email = ?
请注意,在您的示例中,这使用 LEFT JOIN
语法而不是逗号连接语法。逗号连接语法在三十年前就已经过时了。 (是的,我知道,时间过得真快。)
注意它使用 table 别名 u
和 b
作为 table 的别名,以缩短查询。
并且,您应该在每次调用数据库函数后检查错误。这样你就可以更容易地抓住这类问题。
$stmt = $conn->prepare($sql);
if ( !$stmt ) {
$message = $conn->error;
/* statement didn't prepare correctly */
}
$stmt->bind_param("s", $sess);
$stmt->execute();
if ($stmt->error){echo "something has gone wrong";}
我是 PHP 和 mySQL 的新手,我目前正在开发一个个人资料页面,我将在其中显示来自一个名为 table 的已登录用户的个人信息
users
并显示他们将来会从我的网站预订的旅行 table:
booking
我正在使用准备好的语句从用户 table 获取数据,这只会 select 登录用户的数据而没有任何问题,这里是代码:
<?php
include_once 'php/connection.php';
$sess = $_SESSION['email'];
$sql="SELECT firstname, lastname, email, phone, birthday, gender FROM users WHERE email = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $sess);
$stmt->execute();
if ($stmt->error){echo "something has gone wrong";}
$result=$stmt->get_result();
while($row = mysqli_fetch_array ( $result, MYSQLI_ASSOC )) {
?>
我在将使用代码显示数据的部分之后关闭连接:
<h2><?php echo $row["firstname"];?></h2>
<?php
}
$conn->close();
?>
但是,当我尝试从两个 table 中获取数据并将其显示在页面上时,我遇到了问题,我已经进行了研究并尝试了很多选项,但它似乎不起作用,在这里是我试过的:
<?php
include_once 'php/connection.php';
$sess = $_SESSION['email'];
$sql="SELECT firstname, lastname, email, phone, birthday, gender
FROM users, booking
WHERE users.email = booking.email AND email = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $sess);
$stmt->execute();
if ($stmt->error){echo "something has gone wrong";}
$result=$stmt->get_result();
while($row = mysqli_fetch_array ( $result, MYSQLI_ASSOC )) {
?>
通过使用这个脚本,它向我显示了这个错误:
Fatal error: Uncaught Error: Call to a member function bind_param() on boolean in /storage/ssd1/136/16588136/public_html/profile.php:82 Stack trace: #0 {main} thrown in /storage/ssd1/136/16588136/public_html/profile.php on line 82
我一整天都在尝试解决这个问题,但我想不出解决办法。有人可以帮我解决这个问题吗?提前致谢。
你非常接近。由于 SQL 中的错误,您的 ->prepare()
调用失败,因此它 returns 的 $stmt
值为 false 而不是真实语句对象。
当您联接多个 table 时,您需要使用 table 名称限定查询中的列名称。
SELECT u.firstname, u.lastname,
u.email, u.phone, u.birthday, u.gender,
b.something
FROM users u
LEFT JOIN booking b ON u.email = b.email
WHERE u.email = ?
请注意,在您的示例中,这使用 LEFT JOIN
语法而不是逗号连接语法。逗号连接语法在三十年前就已经过时了。 (是的,我知道,时间过得真快。)
注意它使用 table 别名 u
和 b
作为 table 的别名,以缩短查询。
并且,您应该在每次调用数据库函数后检查错误。这样你就可以更容易地抓住这类问题。
$stmt = $conn->prepare($sql);
if ( !$stmt ) {
$message = $conn->error;
/* statement didn't prepare correctly */
}
$stmt->bind_param("s", $sess);
$stmt->execute();
if ($stmt->error){echo "something has gone wrong";}