【发布时间】: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。
-
好吧...我会研究这些东西。我还是不知道他们是什么关系。谢谢你告诉我先生