即使sql是正确的,第二个prepare语句也不起作用

时间:2018-05-03 09:53:37

标签: php mysql prepared-statement

我的搜索功能存在问题。

在首次点击搜索的时候,在帖子表中按帖子标题,帖子作者和帖子标签搜索。当我们没有任何结果时,我想按类别表中的类别名称进行搜索。

当第一次搜索查询没有结果时,我们从类别表category_id获取并尝试通过第3次查询中的post table中的category_id帖子找到。

不幸的是,当它遇到第二个查询时,我会回到字符串:“没有搜索结果。请稍后再试。”。

此外,我得到第二个查询字符串用于调试目的,它是:

SELECT cat_id FROM categories WHERE cat_title LIKE '%HTML%'

当我在phpmyadmin中运行此查询时,它会返回1行,但是在我的php代码中它不起作用。不知道为什么。也许这个准备语句有问题,例如使用close方法。

我在我的代码中使用3 stmt查询。这是我的代码:

if(isset($_POST['search']) && !empty($_POST['search'])) {

        $search = escape($_POST['search']);
        $search = "%{$search}%";

   //FIRST WE ARE LOOKING BY TITLE, TAGS AND AUTHOR.

   $stmt = mysqli_prepare($connection, "SELECT post_id, post_title, post_author, post_date, post_image, post_content, post_tags FROM " . DB_PREFIX . "posts WHERE post_tags LIKE ? OR post_title LIKE ? OR post_author LIKE ?");

   mysqli_stmt_bind_param($stmt, "sss", $search, $search, $search);
   mysqli_stmt_execute($stmt);
   confirmQuery($stmt);
   mysqli_stmt_bind_result($stmt, $post_id, $post_title, $post_author, $post_date, $post_image, $post_content, $post_tags);

   mysqli_stmt_store_result($stmt);  
   if(mysqli_stmt_num_rows($stmt) === 0) {
           mysqli_stmt_close($stmt);

           //NOW WE ARE LOOKING BY CATEGORY

           $search = escape($_POST['search']);
           $search = "'%{$search}%'";

           $stmt1 = mysqli_prepare($connection, "SELECT cat_id FROM " . DB_PREFIX . "categories WHERE cat_title LIKE ?");
           mysqli_stmt_bind_param($stmt1, "s", $search);
           mysqli_stmt_execute($stmt1);
           confirmQuery($stmt1);
           mysqli_stmt_bind_result($stmt1, $cat_id);
           mysqli_stmt_fetch($stmt1);
           mysqli_stmt_store_result($stmt1);  
           if(mysqli_stmt_num_rows($stmt1) === 0) {

                //NO RESULT IN FIRST SEARCH AND SECOND SEARCH QUERY                

                mysqli_stmt_close($stmt1);

                echo "<h1 class='search-no-result'>No Result of Search. Try again later.</h1>";

                //DELETE THIS LATER.
                echo "<h1 class='search-no-result'>SELECT cat_id FROM " . DB_PREFIX . "categories WHERE cat_title LIKE {$search}</h1>";
                //--------------------
                $no_result = true;

           //IF THERE IS CATEGORY LIKE SEARCHED
           } else {

           //WE ARE LOOKING FOR POST FROM THIS CATEGORY

           $stmt2 = mysqli_prepare($connection, "SELECT post_id, post_title, post_author, post_date, post_image, post_content, post_tags FROM " . DB_PREFIX . "posts WHERE post_category_id = ?");

           mysqli_stmt_bind_param($stmt2, "i", $cat_id);
           mysqli_stmt_execute($stmt2);
           confirmQuery($stmt2);
           mysqli_stmt_bind_result($stmt2, $post_id, $post_title, $post_author, $post_date, $post_image, $post_content, $post_tags);
        }
     } else {
           $no_result = false;
     }
   } else {
       redirect("/Udemy/cms/index");
 }

此外,我可以说搜索适用于标题,作者,标签搜索。只有类别失败。

那么问题是什么?

stmt查询或其他什么问题?

// EDIT

我将第二个查询更改为:

$sql = "SELECT COUNT(*) AS cnt FROM " . DB_PREFIX . "categories WHERE cat_title LIKE ?";

结果是:

  

没有搜索结果。稍后再试。 SELECT COUNT(*)AS cnt FROM   categories WHERE cat_title LIKE'%html%'| cnt为0

显然sql查询出了问题,但是当我在phpmyadmin中输入这个查询时,我得到了这个结果:

  

cnt 1

我认为错误是因为我在第一次之后做了第二次准备声明,但我认为$ stmt-&gt; close()会使它变好。也许情况确实如此,但我不确定。

您有什么想法可以解决这个问题吗?

0 个答案:

没有答案