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

时间:2015-06-28 13:44:24

标签: php mysqli

我是Php的新手,我试图将参数传递给从我维护的表中获取的SQL语句。传递的参数可以是product_id,日期或在这种情况下设置限制。这是我到目前为止所做的......需要建议解决这个问题..

存储在表web_query中的DB语句:

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

$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
}

1 个答案:

答案 0 :(得分:0)

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

此外,您不能混合使用forural和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";