gpt4 book ai didi

php - PHP中如何将foreach转换为if

转载 作者:行者123 更新时间:2023-12-02 21:57:19 25 4
gpt4 key购买 nike

我想将 foreach 转换为 if 语句,因为 mysql 查询只返回一行。所以我想使用 if 而不是 foreach

    $db = new db();
$db -> serverconnection();

$item = $_GET["name"];

$query = "SELECT c.firstname,c.phonenumber,c.address FROM `order` o,customerdetails c WHERE o.ordercode = '$item' AND c.sno=o.customerid";

$results = $db->selectQuery($query);


foreach ($results as $customerdetails)
{
$orderidDetail = $customerdetails['customerid'];
echo $customerdetails["firstname"];
echo $customerdetails["phonenumber"];
echo $customerdetails["address"];
}

最佳答案

使用current .

$customerdetails = current($results);
if($customerdetails)
{
$orderidDetail = $customerdetails['customerid'];
echo $customerdetails["firstname"];
echo $customerdetails["phonenumber"];
echo $customerdetails["address"];
}

关于php - PHP中如何将foreach转换为if,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17531597/

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