Read Return SELECT Array

Asked

Viewed 48 times

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":[]}

Retorno do meu select

2 answers

0

You are using the function mysqli_fetch_array which requires a class object as the first parameter mysql_result - as you can see in the documentation here. But you are passing $data containing an object of the class mysqli_stmt - this object was returned by the function mysqli::prepare on line 2 (line 158 of your file).

You have two options:

OPTION 1

Above mysqli_fetch_array (line 170 of your file), you invoke the method get_result class mysqli_stmt. It will return you an object of the class mysqli_result.

$result = $data->get_result();
$row = mysqli_fetch_array($result);
// ...

But I recommend option 2.

OPTION 2

Since you are already dealing with Object Orientation (class mysqli, mysqli_stmt) it is more coherent and recommendable to deal with the class mysqli_result. Instead of using the function mysqli_fetch_array, you can use the method fetch_array class mysqli_result. That function and that method have the same function as you can see in documentation.

$result = $data->get_result();  // objeto da classe mysqli_result 
$row = $result->fetch_array();

The error of line 171 is due to $row be of value null due to line error 170.


The error of line 177 is for the same reason as line 170. You can use the method mysqli_result::fetch_row.

Procedural

 while(($row = mysqli_fetch_row($result))) {
// ...

POO (RECOMMENDED)

 while(($row = $result->fetch_row())) {
// ...
  • See about using one of the flags MYSQLI_ASSOC or MYSQLI_NUM in the method fetch_array because without flags the function returns an array with duplicated data (with numeric indexes and with indexes that have the BD column name).

  • the duplicity already dealt with in select

  • got it another way, I’ll post

  • This duplicity is not related to Mysql SELECT, but rather how fetch_array works.

0


Solution was to use the fetchAll() and foreach($Row the $r)

$data->execute();
$row = $data->fetchAll();
if( $row == null ){
    $users['message'] = "Sem assessorias.";
    http_response_code(400);
    echo json_encode($users);
    exit();
}
else{
  foreach($row as $r){
      $users[] = array(
        'idTrabalhosCientificos' => $r['id_trabalhos_cientificos']
                      );
 }
}
  • You are using PDO or mysqli?

  • $Conn = new mysqli($servername, $username, $password, $database);

  • Does that answer? or not... ?

  • You are using mysqli then, but the class mysqli does not have the method fetchAll. Is your code collecting BD data? No line error $row = $data->fetchAll();?

  • Sorry @Wesleygonçalves am using PDO.... class Connect extends PDO

  • I assumed you were using mysqli because he was using the functions mysqli_fetch_array, mysqli_fetch_row that have to do with Mysql Improved Extension (classes mysqli mysql_stmt, mysqli_result. In this case, you need to use class functions/methods PDO and PDOStatement

  • I’ll read about it.... Valew by @Wesleygonçalves help

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.