I want to convert foreach to if statement because mysql query returns only one row.So i want to use if instead of 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"];
}
Use current.
$customerdetails = current($results);
if($customerdetails)
{
$orderidDetail = $customerdetails['customerid'];
echo $customerdetails["firstname"];
echo $customerdetails["phonenumber"];
echo $customerdetails["address"];
}
Something like
if (count($results) == 1) {
... single row of results ...
} else {
foreach(...) { ... multiple rows ...}
}
perhaps?
But if you KNOW the query will only ever return one row, then you don't really need the if/foreach at all anyways.
change
foreach ($results as $customerdetails)
{
$orderidDetail = $customerdetails['customerid'];
echo $customerdetails["firstname"];
echo $customerdetails["phonenumber"];
echo $customerdetails["address"];
}
to
if(isset($results[0]))
{
$orderidDetail = $results[0]['customerid'];
echo $results[0]["firstname"];
echo $results[0]["phonenumber"];
echo $results[0]["address"];
}