【问题标题】:PDO/OOP Search Engine phpPDO/OOP 搜索引擎 php
【发布时间】:2014-03-19 10:39:07
【问题描述】:

我似乎无法在代码中指出错误:

这是函数发生和执行的地方。这是来自不同的文件

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;
    }

这部分应该检查和结果。再次不同的文件

 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;
    }
}

这是执行时的错误

*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

我想知道如何解决这个错误。

【问题讨论】:

  • 你认为$title 来自哪里?正如错误消息所说,它不存在。为什么要在 PDO 结果上使用 mysql_ 函数?
  • 哦,是的,对不起,我没有意识到这一点。在 PDO 结果上使用 msql_ 函数有什么问题?
  • mysql_ 函数仅适用于 mysql_ 函数产生的值/变量。 PDO 是一个完全不同的 API。
  • 好吧...我会研究这些东西。我还是不知道他们是什么关系。谢谢你告诉我先生

标签: php mysql oop pdo


【解决方案1】:

你做错了两件事:

  • 首先:您在使用 PDO 时尝试使用 mysql_fetch_rows()。一个用于mysql,另一个用于PDO。所以,它们是两个完全不同的结构,所以不要混淆它们。

  • 第二:您假设所有SELECT 语句都将返回一些值,只需在之后执行fetchAll

    *查询不成功怎么办? fetchAll() 怎么能得到你什么?

如果您想检查您的SELECT 语句是否返回任何值,您可以使用rowCount() 方法从函数中执行此操作。

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;
}

或者在你的函数之外代替mysql_fetch_rows,你可以检查做:

$results = $code->search($title, "book_info");
if($result){
 //query is ok
}

【讨论】:

  • 谢谢先生。它可以工作,但不会显示结果。我想问题出在我的数据库上。我可以从这里处理它。非常感谢你。 :D (y)
  • 我已经编辑了答案。再次检查脚本。很可能是你要求mysql给你一个不存在的数据。
  • 是的..现在很好..我的数据库是空的..我没有意识到。再次感谢先生。 :)
【解决方案2】:

一个正确的版本

public function search($title)
{
    $q = "SELECT * FROM table WHERE title LIKE ?";
    $stmt = $this->con->prepare($q);
    $stmt->execute(array("%$title%"));
    return $stmt->fetchAll(PDO::FETCH_ASSOC);
}

请注意,表和字段名称在查询中按原样硬编码

【讨论】:

  • 顺便说一句:我认为你假设表/字段名称应该是硬编码的是错误的。你从哪里读到的?
【解决方案3】:

试试这个代码

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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-03
    • 1970-01-01
    • 2011-03-25
    • 1970-01-01
    • 2018-12-20
    • 2012-06-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多