gpt4 book ai didi

php - 如何在 while 循环中获取某些数据而不重新分配

转载 作者:行者123 更新时间:2023-11-29 10:36:57 27 4
gpt4 key购买 nike

我尝试将数据的名字和姓氏移出循环,但没有显示任何数据。我想知道是否可以只重复产品、价格、数量而不重复名字和姓氏?

<?php
include ('dbconnect.php');
$sql='SELECT * FROM `user_info` ,`customer_order` WHERE user_info.user_id=customer_order.uid';

$run_query=mysqli_query($conn,$sql);


if(! $run_query ) {
die('Could not get data: ' . mysqli_error());
}

while($row = mysqli_fetch_array($run_query, MYSQLI_ASSOC)) {
echo "name:{$row['first_name']} ".
"Last:{$row['last_name']} <br> ".
"Product:{$row['p_name']} <br> ".
"Price:{$row['p_price']} <br> " .
"Quanity:{$row['p_qty']} <br> ";

}

最佳答案

为了简化上面显示的代码,并且由于 SQL 语句执行起来相对较快,我建议将语句分成两部分:首先获取名字/姓氏,然后运行一个循环来获取以下列表:您需要的细胞。

<?php
include ('dbconnect.php');
// Fetch user info
$sql='SELECT * FROM `user_info` WHERE user_info.user_id=customer_order.uid';
$run_query=mysqli_query($conn,$sql);

if(! $run_query ) {
die('Could not get data: ' . mysqli_error());
}
$row = mysqli_fetch_array($run_query, MYSQLI_ASSOC);
echo "Customer: name:{$row['first_name']} ".
"Last:{$row['last_name']} <br>" ;

// Fetch every orders needed
$sql='SELECT `p_name`, `p_price`, `p_qty` FROM `user_info`, `customer_order` WHERE `user_info.user_id=customer_order.uid';
$run_query=mysqli_query($conn,$sql);
if(! $run_query ) {
die('Could not get data: ' . mysqli_error());
}

while($row = mysqli_fetch_array($run_query, MYSQLI_ASSOC))
echo "Product:{$row['p_name']} <br> ".
"Price:{$row['p_price']} <br> " .
"Quantity:{$row['p_qty']} <br> ";
}

关于php - 如何在 while 循环中获取某些数据而不重新分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46244752/

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