I am just starting learning codes and am writing some codes to echo an array and it give me this error "Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\ogmtest_server_api.php on line 62" HOW CAN I SOLVE THIS? This is my code:
if($Result1){
// script to get business no, amount & merchant id,output to merchant page
$query="SELECT * FROM customer_order WHERE insert_time=(SELECT max(order_time)from customer_order)";
$result=mysql_query($query);
while($row=mysql_fetch_assoc($result)){
$amount=$row['amount'];
$id=$row['merchant_id'];
$payment_mode=$row['mobile_service'];
switch($payment_mode){
case 'TIGO-PESA':
$result1=mysql_query("SELECT * FROM mobile_client WHERE mobile_service='TIGO-PESA'");
while($row1=mysql_fetch_assoc($result1)){
$data=array(
'Business no'=>$row1['business_no'],
'Payment Mode'=>$payment_mode,
'Total Amount Tsh'=>$amount,
'Merchant ID'=>$id
);
}break;
case 'M-PESA':
$result1=mysql_query("SELECT * FROM mobile_client WHERE mobile_service='M-PESA'");
while($row1=mysql_fetch_assoc($result1)){
$data=array(
'Business no'=>$row1['business_no'],
'Payment Mode'=>$payment_mode,
'Total Amount Tsh'=>$amount,
'Merchant ID'=>$id
);
}break;
case 'AIRTEL-MONEY':
$result1=mysql_query("SELECT * FROM mobile_client WHERE mobile_service='AIRTEL-MONEY'");
while($row1=mysql_fetch_assoc($result1)){
$data=array(
'Business no'=>$row1['business_no'],
'Payment Mode'=>$payment_mode,
'Total Amount Tsh'=>$amount,
'Merchant ID'=>$id
);
}break;
default:
$data=array('error'=>"no payment mode selected");
}
}
foreach( $data as $value ){
echo $value;
}
}
else{
echo "wrong";
}
}
you can use print_r() to print the array. like this:
echo '<pre>';
print_r($data);
echo '</pre>';
if $data is an array. So you can debug it by yourself.
Note that first declare $data as array at the top of script. like: $data = array();
You can use print_r()
, but it does not always output the type. It is most useful when you already know that the variable holds an array.
Use var_dump()
here instead. You may have to use a pre
element, as arslaan suggested. With var_dump()
and the Xdebug extension enabled, you do not have to do that either and the output will be highlighted.