0
I need to return an array in JSON format.
Format is not the problem.
The problem is to take the $Row index and display.
$db = new Connect;
$data = $db->prepare("SELECT * FROM trabalhos_cientificos");
$data->execute();
$row = $data->fetch();
if( $row == null ){
$users['message'] = "Sem assessorias.";
http_response_code(400);
echo json_encode($users);
exit();
}else{
//$row = mysqli_fetch_array($data->fetch());
$row = mysqli_fetch_array($data); //line 170
foreach($row as $r) { //line 171
$users[] = $r;
echo json_encode($r['id_trabalhos_cientificos']);
}
echo json_encode(array('Data1' => $users));
while(($row = mysqli_fetch_row($data))) { //line 177
$users[] = $row;
echo json_encode($row['id_trabalhos_cientificos']);
}
echo json_encode(array('Data2' => $users));
http_response_code(200);
//echo json_encode($users);
//echo json_encode(array($users));
exit();
}
Warning: mysqli_fetch_array() expects Parameter 1 to be mysqli_result, array Given in /opt/lampp/htdocs/.../logated.php online 170
Warning: Invalid argument supplied for foreach() in /opt/lampp/htdocs/.../logated.php online 171
{"Data1":[]}
Warning: mysqli_fetch_row() expects Parameter 1 to be mysqli_result, Object Given in /opt/lampp/.../logged in.php online 177
{"Data2":[]}
See about using one of the flags
MYSQLI_ASSOC
orMYSQLI_NUM
in the methodfetch_array
because without flags the function returns an array with duplicated data (with numeric indexes and with indexes that have the BD column name).– Wesley Gonçalves
the duplicity already dealt with in select
– FlipNovid
got it another way, I’ll post
– FlipNovid
This duplicity is not related to Mysql SELECT, but rather how
fetch_array
works.– Wesley Gonçalves