如何在Php中将foreach转换为if
我想把foreach转换成if语句,因为mysql查询只返回一行,所以我想用if代替foreach如何在Php中将foreach转换为if,php,if-statement,for-loop,Php,If Statement,For Loop,我想把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'
$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"];
}
使用
差不多
if (count($results) == 1) {
... single row of results ...
} else {
foreach(...) { ... multiple rows ...}
}
也许吧
但是如果您知道查询只返回一行,那么无论如何您都不需要if/foreach
foreach ($results as $customerdetails)
{
$orderidDetail = $customerdetails['customerid'];
echo $customerdetails["firstname"];
echo $customerdetails["phonenumber"];
echo $customerdetails["address"];
}
到
}如果是,目的是什么?
foreach ($results as $customerdetails)
{
$orderidDetail = $customerdetails['customerid'];
echo $customerdetails["firstname"];
echo $customerdetails["phonenumber"];
echo $customerdetails["address"];
if(isset($results[0]))
{
$orderidDetail = $results[0]['customerid'];
echo $results[0]["firstname"];
echo $results[0]["phonenumber"];
echo $results[0]["address"];