dtlzdofl66441 2014-03-19 10:39
浏览 45
已采纳

PDO / OOP搜索引擎php

I can't seem to place what is wrong in the code :

This is where the function takes place and execute. this is from a different file

public function search($title, $table)
    {

        $q = "SELECT * FROM $table WHERE (':title' LIKE '%".$title."%')";
        $stmt = $this->con->prepare($q);
        $stmt->execute(array(':title' => $title));
        $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
        return $result;
    }

This part should check and results. different file again

 if(isset($_POST['submit'])){
     $search = $_POST['search'];   
     $min_length = 2;

    if(strlen($search) >= $min_length){

        $search = htmlspecialchars($search); 
        $results = $code->search($title, "book_info"); //the error is here*

        if(mysql_num_rows($results) > 0){ // the error is here too**

            while($results != $code->fetchAll()){
            echo "<table id=\"tablecolor\" class=\"echoname\" >";
            echo "<th><b>ID</b></th>";
            echo "<th><b>Title</b></th>";
            echo "<th><b>Author</b></th>";
            echo "<th><b>ISBN</b></th>";
            echo "<th><b>Publisher</b></th>";
            echo "<th><b>Language</b></th>";
            echo "<th><b>Genre</b></th>";
            echo "<th><b>Quantity</b></th>";
            echo "<pre>";  
                    echo "<tr>";
                    echo "<td>".$id."</td>";
                    echo "<td>".$title."</td>";
                    echo "<td>".$author."</td>";
                    echo "<td>".$isbn."</td>";
                    echo "<td>".$publisher."</td>";
                    echo "<td>".$language."</td>";
                    echo "<td>".$genre."</td>";
                    echo "<td><center>".$quantity."</center></td>";
                    echo "</tr>";    
            echo "</pre>";
            echo "</table>";

            }

        }
        else{ 
            echo "No results";
        }

    }
    else{ 
        echo "Minimum length is ".$min_length;
    }
}

This is the error when executed

*Notice: Undefined variable: title in C:\wamp\www\unitato\web\user\user-search1.php on line 16
**Warning: mysql_num_rows() expects parameter 1 to be resource, array given in C:\wamp\www\unitato\web\user\user-search1.php on line 18

I want to know how to fix this error.

  • 写回答

3条回答 默认 最新

  • doubi4617 2014-03-19 10:44
    关注

    You are doing two things wrong:

    • First: You are trying to use mysql_fetch_rows() when you are using PDO. One is for mysql other is for PDO. So, They are two completely different constructs, so don't mix them up.

    • Second: You are assuming all SELECT statement is going to return some value, by simply doing fetchAll afterwards.

      *What if the query was not successful ? How can fetchAll() get you anything?

    If you want to check your SELECT statement is returning any value you can do this from the function using rowCount() method.

    public function search($title, $table)
    {
            try{
            $q = "SELECT * FROM $table WHERE title LIKE ?";
            $stmt = $this->con->prepare($q);
            $stmt->execute(array("%$title%");
            }catch(PDOException $e){
             throw new Exception("ERROR:". $e->getMessage()); 
            }
            if(!$stmt->rowCount()){
              return false; #this will return false if data isn't found. 
            }
            $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
            return $result;
    }
    

    Or instead of mysql_fetch_rows outside you function you can check do:

    $results = $code->search($title, "book_info");
    if($result){
     //query is ok
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度