php将参数传递给从数据库派生的sql语句


php pass parameters to sql statement derived from the database

我是Php的新手,我正试图将参数传递给从我维护的表中获得的SQL语句。传递的参数可以是product_id、日期,或者在本例中为设置限制。这是我目前为止得到的。。。需要建议来解决这个问题。。

存储在表中的DB语句web_query:

SELECT * FROM adhoc_signal.`table 2` limit :startnum , :endnum

PHP脚本:

<?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->field_count;
                 print "Value of stmt : $stmt magnum : $pagnum";
                 }
                 else {
                 print "Prepare statement is returning : False";
                 }
}

或者,如果我将DB SQL语句更改为:select*from adhoc_signal。table 2和在不准备Php脚本的情况下修改Php脚本,它们似乎工作得很好。

<?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->field_count;
             if ($num > 0) {
             print '<div class="panel panel-primary"> ';
             print '<div class="panel-heading"> ';
             print '<h3 class="panel-title">Results</h3> ';
             print '</div>';          
             print '<div id="gentable" class="table-responsive"> ';
             print '<table class="table-responsive table-bordered table-condensed table-hover" > ';
             print '<th><td><strong>Preferred Term</strong></td></th>';    
             while ($row = mysqli_fetch_array($result) ) {
               print " <tr> 'n";
               foreach ($row as $field ){
                print " <td>$field</td> 'n";
               } // end foreach
              print '</tr>';              
             } // end of while        
              print "</table>"; 
              print "</div>"; // End of panel body 
              print "</div>"; 
            }  // end of if
}

绑定参数后,您需要执行准备好的语句,而不是执行$pagres = mysqli_query($con, $stmt);

此外,您不能混合使用过程调用方法和OO调用方法。

if ($stmt = mysqli_prepare($con, $rows['qry_text'])) {
    mysqli_stmt_bind_param($stmt,'sssd', $start_page, $per_page);
     //mysqli_query($con, $stmt);
    mysqli_stmt_execute($stmt);
    $pagnum = mysqli_stmt_num_rows($stmt);
    $pagnumcol= mysqli_stmt_field_count($stmt);
}

此外,我认为你不想在这行中打印$stmt

print "Value of stmt : $stmt magnum : $pagnum";