title: Select multiple rows into array and send them via AJAX
body: <p>I am trying to run a <code>SELECT</code> query in PHP and then multiple rows are selected, but I need to fetch them into an array and then use: <code>echo json_encode($array)</code>. After That I need to get this array into AJAX.</p>
  
  <p>Here is the PHP code:</p>
  
  <pre><code>$val = $_POST['data1'];
  $search = "SELECT * FROM employee WHERE emp_name = :val OR salary = :val OR date_employed = :val";
  $insertStmt = $conn-&gt;prepare($search);
  $insertStmt-&gt;bindValue(":val", $val);
  $insertStmt-&gt;execute();
  $insertStmt-&gt;fetchAll();
  
  //echo "success";
  //$lastid = $conn-&gt;lastInsertId();
  $i = 0;
  foreach($insertStmt as $row)
  {
      $arr[$i] = $row;
      $i++;
  }
  
  echo json_encode($arr);
  </code></pre>
  
  <p>The problem is that I can't get all the lines of this array into AJAX so I can append them into some table. Here is the script:</p>
  
  <pre><code>var txt = $("#txtSearch").val();
  $.ajax({
      url: 'search.php', // Sending variable emp, pos, and sal, into this url
      type: 'POST', // I will get variable and use them inside my PHP code using $_POST['emp']
      data: {
          data1: txt
      }, //Now we can use $_POST[data1];
      dataType: "json", // text or html or json or script
      success: function(arr) {
          for() {
              // Here I don't know how to get the rows and display them in a table
          }
      },
      error:function(arr) {
          alert("data not added");
      }
  });
  </code></pre>