【发布时间】:2014-01-03 00:11:12
【问题描述】:
我正在开发一个非常基本的 php/mysql 搜索。搜索基于从下拉菜单中选择的值的过滤条件。对于此示例,我将过滤器限制为仅搜索所有表。这些表是blog 和pages。我已经能够使用 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 你能告诉我答案格式吗?不确定我是否完全关注你