我正在开发一个非常基本的php / mysql搜索。搜索基于从下拉菜单中选择的值的过滤条件。对于此示例,我将过滤器限制为仅搜索所有表。表格为blog
和pages
。我已经能够使用php mysqli
样式进行全功能搜索,但是在使用PDO
样式时我一直在墙上运行..对于使用mysqli
的搜索,{{1}执行后跟mysqli_query
来计算结果的行数。主要问题如下:
这种方法与PDO风格略有不同,因为我无法使用mysqli_num_rows
。我必须以HERE显示的方式使用rowCount
。 PDO sytle搜索的结果不正确,无法正常显示?使用关键字fetchColumn
测试两次搜索。正确的搜索将显示13个结果。
SEARCH-1 - Mysqli风格
SEARCH-2 - PDOstyle
的mysqli
blah
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 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";
}
}
HTML 的
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";
}
}
}
答案 0 :(得分: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中,使用预处理语句都是一个好习惯。