the warning is
Warning: count(): Parameter must be an array or an object that implements Countable in C:\xampp\htdocs\restcordauth\output.php on line 9
<?php
$client = curl_init('https://localhost/restcordauth/apiHandler.php?action=outputData');
curl_setopt($client, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($client);
$result = json_decode($response);
$output = '';
if(count($result) > 0){
foreach($result as $row){
$output .='
<tr>
<td>'.$row->userid.'</td>
<td>'.$row->username.'</td>
<td>'.$row->token.'</td>
<td>'.$row->genby.'</td>
<td>'.$row->creation.'</td>
<td>'.$row->sub.'</td>
<td>'.$row->admin.'</td>
</tr>
';
}
}else{
$output .= '<tr><td colspan="7" align="center">not found!</td></tr>';
}
?>
CodePudding user response:
Use this code:
if(is_countable($result) && count($result) > 0){
CodePudding user response:
The warning means you are trying to loop through a variable that is not an array. That is, it's not loopable/countable.
First, be sure the response "result" you are getting from the curl request is an array.
It could be there was an error or the server for many possible reasons, did not give you the expected results so check if the result countable as suggested above with the following
if(is_countable($result) && count($result) > 0){ // do your foreach loop here } else{ $output .= '<tr><td colspan="7" align="center">not found!</td</tr>'; }