I'm trying to get the number of columns on a SQL query result. Whenever I try to use count($row)
the table should have only 4 elements but it shows 8, and the implode(",",$row)
show that in fact there are 8 elements but the output of that is a bit odd:
Code fragment:
$query = $con->query($sql);
$rows = array();
error_log($sql);
if(!$query){error_log(mysqli_error($con));exit;}
while($row = $query->fetch_array()){
$rows[]=$row;
}
if(count($rows)==0) { echo "101"; exit; }
echo '{"row":[';
foreach($rows as $key=>$row) {
error_log(count($row));
error_log(implode(",",$row));
error_log($row[0]." ".$row[1]." ".$row[2]." ".$row[3]);
}
echo "]}";
Result:
[Tue May 29 13:47:21 2018] 8
[Tue May 29 13:47:21 2018] AFI,AFI,001,001,Pancho,Pancho,a1d7584daaca4738d499ad7082886b01117275d8,a1d7584daaca4738d499ad7082886b01117275d8
[Tue May 29 13:47:21 2018] AFI 001 Pancho a1d7584daaca4738d499ad7082886b01117275d8
As you can see, the eight elements are there but they are just duplicates of the correct output, represented by error_log($row[0]." ".$row[1]." ".$row[2]." ".$row[3]);
.
Does anyone know why PHP does it like this? Is there a way to get the correct number of elements on the query row?