显示INNER JOIN中的特定行 - mysqli

时间:2017-06-06 07:55:43

标签: php session mysqli inner-join backend

我正在尝试显示有关当前登录用户的信息。问题是我需要的信息在两个不同的表中,所以我不得不使用INNER JOIN,我有问题显示数据。

<?php 
$email = $_SESSION['email'];

$sql_address = 
"SELECT `customer`.`email`, `address`.`first_name`, `address`.`last_name` 
FROM `customer` INNER JOIN address ON `customer`.`customer_ID`=`address`.`customer_ID WHERE email =". $email; 
$result_address = $db->query($sql_address);


if(isset($_SESSION['email'])){ 
while($row = mysqli_fetch_array($result_address)){ ?>
                         <div class="col-sm-4 mb-20">
                            <div class="mb-20 mb-md-10">
                               <h3> CUSTOMER DETAILS</h3>
                               <p>Name: <?php echo $row['first_name']. " " .$row['last_name'];?><br/>
                                  Email: <?php echo $row['email'];?>
                               </p>
                            </div>
                         </div>
                         <?php }?>

此刻我收到此错误:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in D:\xamp\htdocs\exercises\website\shipment.php on line 149
你能告诉我哪里有问题吗?您还有任何建议如何使其更安全吗?我想将$email放入query可能不是最好的主意。

1 个答案:

答案 0 :(得分:0)

可能你在sql语句中有错误,所以$ result_address为false。 $ email应该是单引号,并且还要向`customer_ID

添加近距离报价

尝试此查询

SELECT `customer`.`email`, `address`.`first_name`, `address`.`last_name` 
FROM `customer` INNER JOIN address ON `customer`.`customer_ID`=`address`.`customer_ID` WHERE email = '". $email . "'";