【问题标题】:Simple Mysql DB Search with PHP pdo - returning incorrect value使用 PHP pdo 进行简单的 Mysql DB 搜索 - 返回不正确的值
【发布时间】:2014-01-03 00:11:12
【问题描述】:

我正在开发一个非常基本的 php/mysql 搜索。搜索基于从下拉菜单中选择的值的过滤条件。对于此示例,我将过滤器限制为仅搜索所有表。这些表是blogpages。我已经能够使用 php mysqli 样式进行功能齐全的搜索,但是在使用 PDO 样式时我会一直跑到一堵墙。对于使用 mysqli 的搜索,会执行 mysqli_query,然后执行 @987654331 @ 统计结果的行数。主要问题如下:

PDO 风格的方法有点不同,因为我不能使用rowCount。我必须以HERE 所示的这种方式使用fetchColumn。 PDO 样式搜索的结果不正确且显示不正确?使用关键字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>

【问题讨论】:

  • (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%') 将成为与另一个数字的数字联合。
  • @MoeTsao 是的,但如果你在下面看到更多,我会再次运行查询而不使用COUNT(*)。它最初用于返回行数的值。更好的描述HERE
  • 这使它成为 8 union 5,所以你有 8 个计数并且没有结果集。使用与mysqli示例相同的查询,使用PHP进行计数应该很容易
  • @MoeTsao 你能告诉我答案格式吗?不确定我是否完全关注你

标签: php mysql pdo


【解决方案1】:

编辑以包含更完整的表格。

与您的 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 中,使用准备好的语句都是一个好习惯。

【讨论】:

  • 我试过这个并没有得到结果。你能展示完整的实现吗?
猜你喜欢
  • 1970-01-01
  • 2011-03-21
  • 1970-01-01
  • 2023-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多