title: Prepared statements in DataTables
body: <p>I want to use prepared statements inside <a href="https://datatables.net/" rel="nofollow noreferrer">DataTables</a> for the code stated below. It has two queries (<code>query</code> and <code>query1</code>).</p>
  <p>Query has two &quot;where conditions&quot; which variables are <code>$classid</code> and <code>$searchfield</code>.
  Query1 is a &quot;Limit&quot; statement and also has two variables <code>$start</code> and <code>$end</code>.</p>
  <p>The results of both queries should be stored in the <code>$data</code> array. At the moment I don't get any results. How do I bind the paramteres correctly in this case and store the results in the array so DataTables gets a working json?</p>
  <p><strong>My try:</strong></p>
  <pre><code>$connect = mysqli_connect(&quot;localhost&quot;, &quot;xxxx&quot;, &quot;xxx&quot;, &quot;xxx&quot;);
  $columns = array('itemOverview.id', 'itemOverview.Display_lang');
  
  // Variables to bind
  
  $classid = 4;
  $searchfield = $_POST[&quot;is_searchfield&quot;];
  $start = $_POST['start'];
  $end = $_POST['length'];
  
  $query = &quot;SELECT * FROM itemOverview INNER JOIN item ON item.id = itemOverview.id&quot;;
  
  $query.= &quot; WHERE item.ClassID = ? AND &quot;;
  
  if(!empty($searchfield)) {
      $query .= &quot;itemOverview.Display_lang = ? AND &quot;;
  }
  
  $query1 = '';
  
  if($_POST[&quot;length&quot;] != -1) {
      $query1 = 'LIMIT ?, ?';
  }
  
  $stmt = mysqli_stmt_init($connect);
      if (!mysqli_stmt_prepare($stmt, $query. $query1)) {
          echo &quot;SQL Failed&quot;;
      } else {
  
          // bind paramaters to the placeholder
          mysqli_stmt_bind_param($stmt, &quot;ssss&quot;, $classid, $searchfield, $start, $end);
  
          // run parameters inside database
          mysqli_stmt_execute($stmt);
          $result = mysqli_stmt_get_result($stmt);
  
          $data = array();
  
          while($row = mysqli_fetch_array($result))
          {
              $sub_array = array();
              $sub_array[] = $row[&quot;id&quot;];
              $sub_array[] = $row[&quot;Display_lang&quot;];
              $data[] = $sub_array;
          }
  }
  
  $output = array(
     &quot;draw&quot;    =&gt; intval($_POST[&quot;draw&quot;]),
     &quot;data&quot;    =&gt; $data
  );
  
  echo json_encode($output);
  </code></pre>