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条)

报告相同问题?

悬赏问题

  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿