Php - multiple "select" queries using prepared statement

1

The following code has the function of receiving multiple page ids, executing a procedure to obtain your data and returning an array with all this information.

The function works fine but only when it is 1 iteration, is there something that is missing? What can cause the error?

function getArrayToPost($pages) {
  $array_response = array();
  if($conn = createConnection()) {

      $stmt = $conn->prepare('call wp_getFbPage(?);');
      $stmt->bind_param('i', $page_id);

      foreach ($pages as $fb_pagina){
          $page_id = (int)$fb_pagina['id'];
          $stmt->execute();
          $result = $stmt->get_result();
          $page_info = $result->fetch_assoc();
          $array_response[] = $page_info;
          // break; <= si es agregado, no lanza error, 1 iteracion
      }
      $conn->close();
      $stmt->close();
  }
  return $array_response;
}

Error: pid 4116 exit signal Segmentation fault (11), possible coredump in     / etc / apache2

    
asked by Angel 24.08.2018 в 00:57
source

1 answer

1

After several tests I finally managed to find the solution, I was previously using mysqli for being quite simple and practical to use, but for this operation I need to use PDO and at the end my code ended like this:

function getFBArrayToPost($pages) {
  $conn = createPdoConnection();
  $array_response = array();

  $stmt = $conn->prepare('call wp_getFbPage(:id);');      
  foreach($pages as $fb_pagina) {
    $page_id = (int)$fb_pagina['id'];
    $stmt->bindValue(':id', $page_id);
    $stmt->execute();
    $page_info = $stmt->fetch(PDO::FETCH_ASSOC);
    $array_response[] = $page_info;
    $stmt->closeCursor();
  }
  $conn = null;
  return $array_response;
}
    
answered by 25.08.2018 / 17:19
source