gpt4 book ai didi

php - 如何使用php在sql中的两个表中获取特定用户的数据

转载 作者:行者123 更新时间:2023-11-29 03:20:49 25 4
gpt4 key购买 nike

从两个表中获取数据已经可以了,但是我不知道如何从两个表中获取用户的特定数据。我不知道将这段代码放在哪里//WHERE seller_id= '". $_SESSION['seller_id'] . "' ");

这是我的代码:

<?php
$seller_id = $_SESSION['seller_id'];
$user_query = mysqli_query($db, "SELECT seller.s_fname, seller.s_lname, seller.trade_name, seller.s_address, selling_details.opening_time, selling_details.opening_days, selling_details.order_cutoff, selling_details.seller_delivery_time, selling_details.area_covered_delivery, selling_details.delivery_fee FROM seller INNER JOIN selling_details ON seller.seller_id=selling_details.seller_id
WHERE seller.seller_id=selling_details.seller_id");
$seller = mysqli_fetch_assoc($user_query);
?>

最佳答案

由于您需要将一些参数嵌入到查询中,我们将使用 prepared statements防止SQL injection攻击。

// Get seller ID
$seller_id = $_SESSION['seller_id'];

// Prepare the query that will be executed
$stmt = $db->prepare("SELECT seller.s_fname AS s_fname, seller.s_lname AS s_lname, seller.trade_name AS trade_name, seller.s_address AS s_address, selling_details.opening_time AS opening_time, selling_details.opening_days AS opening_days, selling_details.order_cutoff AS order_cutoff, selling_details.seller_delivery_time AS seller_delivery_time, selling_details.area_covered_delivery AS area_covered_delivery, selling_details.delivery_fee AS delivery_fee FROM seller INNER JOIN selling_details ON seller.seller_id=selling_details.seller_id WHERE seller.seller_id = ?");

// Instead of that '?', put the $seller_id
$stmt->bind_param('i', $seller_id);

// Execute the statement.
$stmt->execute();

// Get the results.
$result = $stmt->get_result();

// For each row, get it's associative array representation.
while ($row = mysqli_fetch_assoc($result)) {
// Echo first and last name.
echo $row['s_fname'].' '.$row['s_lname'].'<br/>';
}

// Close the statement.
$stmt->close();

关于php - 如何使用php在sql中的两个表中获取特定用户的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45538293/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com