簡體   English   中英

使用PHP pdo的簡單Mysql DB搜索-返回不正確的值

[英]Simple Mysql DB Search with PHP pdo - returning incorrect value

我正在開發一個非常基本的php / mysql搜索。 搜索基於從下拉菜單中選擇的值的過濾條件。 對於此示例,我將過濾器限制為僅搜索所有表。 這些表是blogpages 我已經能夠使用php mysqli風格進行全功能搜索,但是使用PDO風格時我一直跑到牆前。對於使用mysqli的搜索,將執行mysqli_query ,然后執行mysqli_num_rows來計算結果的行數。 主要問題如下:

對於PDO樣式,該方法有些不同,因為我不能使用rowCount 我必須按照此處所示的方式使用fetchColumn PDO sytle搜索的結果不正確並且顯示不正確? 使用關鍵字blah測試兩個搜索。 正確的搜索將顯示13個結果。

SEARCH-1 -Mysqli樣式

SEARCH-2 -PDOstyle

mysqli的

include("db_con/db_con.php");
$search_output = "";
if(isset($_POST['searchquery']) && $_POST['searchquery'] != ""){
  $searchquery = preg_replace('#[^a-z 0-9?!]#i', '', $_POST['searchquery']);
  if($_POST['filter1'] == "All Tables"){
    $sqlCommand = "(SELECT id, page_title AS title FROM pages WHERE page_title LIKE '%$searchquery%' OR page_body LIKE '%$searchquery%') UNION (SELECT id, blog_title AS title FROM blog WHERE blog_title LIKE '%$searchquery%' OR blog_body LIKE '%$searchquery%')";
  }
  $query = mysqli_query($db_conx, $sqlCommand) or die(mysql_error());
  $count = mysqli_num_rows($query);
  if($count > 0){
    $search_output .= "<hr />$count results for <strong>$searchquery</strong><hr />$sqlCommand<hr />";
    while($row = mysqli_fetch_array($query)){
              $id = $row["id"];
        $title = $row["title"];
        $search_output .= "Item ID: $id - $title<br />";
                } // close while
  } else {
    $search_output = "<hr />0 results for <strong>$searchquery</strong><hr />$sqlCommand";
  }
}

PDO

include("db_con/db_con.php");
$search_output = "";
if(isset($_POST['searchquery']) && $_POST['searchquery'] != ""){
  $searchquery = preg_replace('#[^a-z 0-9?!]#i', '', $_POST['searchquery']);
  if($_POST['filter1'] == "All Tables"){
   $sqlCommand = "(SELECT COUNT(*) FROM pages WHERE page_title LIKE '%$searchquery%' OR page_body LIKE '%$searchquery%') UNION (SELECT COUNT(*) FROM blog WHERE blog_title LIKE '%$searchquery%' OR blog_body LIKE '%$searchquery%')";
    $sql_prepare = $db_con->prepare($sqlCommand);
  }
  if($sql_prepare->execute()){
    $count = $sql_prepare->fetchColumn();
    if($count > 1){
      if(isset($_POST['searchquery']) && $_POST['searchquery'] != ""){
        $searchquery = preg_replace('#[^a-z 0-9?!]#i', '', $_POST['searchquery']);
        if($_POST['filter1'] == "Whole Site"){
         $sqlCommand = "(SELECT id, page_title AS title FROM pages WHERE page_title LIKE '%$searchquery%' OR page_body LIKE '%$searchquery%') UNION (SELECT  id, blog_title AS title FROM blog WHERE blog_title LIKE '%$searchquery%' OR blog_body LIKE '%$searchquery%')";
          $sql_prepare = $db_con->prepare($sqlCommand);
        }
      }
      $search_output .= "<hr />$count results for <strong>$searchquery</strong><hr />$sqlCommand<hr />";
      $query = $sql_prepare->fetchAll();
      foreach($query as $row){
          $id = $row["id"];
          $title = $row["title"];
          $search_output .= "Item ID: $id - $title<br />";
          } // close while
    } else {
      $search_output = "<hr />0 results for <strong>$searchquery</strong><hr />$sqlCommand";
    }
  }
}

HTML

<form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>">
Search For:
  <input name="searchquery" type="text" size="44" maxlength="88">
Within:
<select name="filter1">
<option value="All Tables">All Tables</option>
</select>
<input name="myBtn" type="submit">
<br />
</form>

編輯以包括更完整的表格。

與mysqli解決方案唯一不同的是如何計算結果集中的項目數。

include("db_con/db_con.php");
$search_output = "";
if(isset($_POST['searchquery']) && $_POST['searchquery'] != ""){
  $searchquery = preg_replace('#[^a-z 0-9?!]#i', '', $_POST['searchquery']);
  if($_POST['filter1'] == "All Tables"){
    $sqlCommand = "(SELECT id, page_title AS title FROM pages WHERE page_title LIKE '%$searchquery%' OR page_body LIKE '%$searchquery%') UNION (SELECT id, blog_title AS title FROM blog WHERE blog_title LIKE '%$searchquery%' OR blog_body LIKE '%$searchquery%')";
  }
  $sql_result = $db_con->query($sqlCommand);
  $query = $sql_prepare->fetchAll();
  $count = count($query);
  $search_output="";
  foreach($query as $row){
    $id = $row["id"];
    $title = $row["title"];
    $search_output .= "Item ID: $id - $title<br />";
  } 
  echo $search_output;
}

當您不使用預處理語句時,則無需執行PDO :: prepare。 但是,在mysqli或PDO中使用預處理語句是一個好習慣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM