title: php pass parameters to sql statement derived from the database
body: <p>I'm newbie to Php and i'm trying to pass parameters to a SQL statement obtained from a table i maintain. The parameters passed could be product_id, dates  or in this case setting Limits. Here is what i've got so far...need advise to fix this.. </p>
  
  <p>DB statement stored in a table web_query: </p>
  
  <pre><code>SELECT * FROM adhoc_signal.`table 2` limit :startnum , :endnum
  </code></pre>
  
  <p>PHP script :</p>
  
  <pre><code>&lt;?php
  
  $con = mysqli_connect("localhost", "my_user", "my_password", "world");
  
  /* check connection */
  if (!$con) {
      printf("Connect failed: %s\n", mysqli_connect_error());
      exit();
  }
  
  //Initialize variables
      $per_page=8;
      if (isset($_GET[“page”])) {
         $page = $_GET[“page”];
       }
      else {
       $page=1;
     } 
  
  // Page will start from 0 and Multiple by Per Page
  $start_from = ($page-1) * $per_page;
  
    $query = mysqli_query($con,"SELECT qry_text from web_query where report_id = 1") or die("Error : Failed to fetch Query records: " . mysql_error());  
  
             if (!$query) {  
                  $error = 'Error fetching reports: ' . mysqli_error($con);  
                  exit();  
             }
                  // echo 'I got some webqry records';
  
                while ($rows = mysqli_fetch_array($query)) {
  
                if (!empty($rows) || (array() !== $rows)) {
                  // Following Problematic prepare statement...                                   
                   if ($stmt = mysqli_prepare($con, $rows['qry_text'])) {
                     mysqli_stmt_bind_param($stmt,'sssd', $start_page, $per_page);
                     $pagres = mysqli_query($con, $stmt);
                     $pagnum = mysqli_num_rows($pagres);
                     $pagnumcol= $pagres-&gt;field_count;
  
                   print "Value of stmt : $stmt magnum : $pagnum";
                   }
                   else {
                   print "Prepare statement is returning : False";
                   }
  }
  </code></pre>
  
  <p>Alternatively if i change the DB SQL statement to : select * from adhoc_signal.<code>table 2</code> and modify the Php script without preparing it, they seem to work fine.</p>
  
  <pre><code>&lt;?php
  
  $con = mysqli_connect("localhost", "my_user", "my_password", "world");
  
  /* check connection */
  if (!$con) {
      printf("Connect failed: %s\n", mysqli_connect_error());
      exit();
  }
  
  //Initialize variables
      $per_page=8;
      if (isset($_GET[“page”])) {
         $page = $_GET[“page”];
       }
      else {
       $page=1;
     } 
  
  // Page will start from 0 and Multiple by Per Page
  $start_from = ($page-1) * $per_page;
  
    $query = mysqli_query($con,"SELECT qry_text from web_query where report_id = 1") or die("Error : Failed to fetch Query records: " . mysql_error());  
  
             if (!$query) {  
                  $error = 'Error fetching reports: ' . mysqli_error($con);  
                  exit();  
             }
                  // echo 'I got some webqry records';
  
                while ($rows = mysqli_fetch_array($query)) {                 
                if (!empty($rows) || (array() !== $rows)) {
               $result = mysqli_query($con, $rows['qry_text']) or die("Error : Why is the Query failing : ". mysql_error());
               $num=mysqli_num_rows($result);
               $numcol= $result-&gt;field_count;
  
               if ($num &gt; 0) {
               print '&lt;div class="panel panel-primary"&gt; ';
               print '&lt;div class="panel-heading"&gt; ';
               print '&lt;h3 class="panel-title"&gt;Results&lt;/h3&gt; ';
               print '&lt;/div&gt;';          
               print '&lt;div id="gentable" class="table-responsive"&gt; ';
               print '&lt;table class="table-responsive table-bordered table-condensed table-hover" &gt; ';
               print '&lt;th&gt;&lt;td&gt;&lt;strong&gt;Preferred Term&lt;/strong&gt;&lt;/td&gt;&lt;/th&gt;';    
  
               while ($row = mysqli_fetch_array($result) ) {
                 print " &lt;tr&gt; \n";
                 foreach ($row as $field ){
                  print " &lt;td&gt;$field&lt;/td&gt; \n";
                 } // end foreach
                print '&lt;/tr&gt;';              
               } // end of while        
                print "&lt;/table&gt;"; 
                print "&lt;/div&gt;"; // End of panel body 
                print "&lt;/div&gt;"; 
              }  // end of if
  }
  </code></pre>