我似乎無法在代碼中發生什麼問題:PDO/OOP搜索引擎php
這是函數發生和執行的地方。這是來自不同的文件
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_函數? – deceze
噢耶對不起,我沒有意識到這一點。在PDO結果中使用msql_函數有什麼問題? – user3406220
mysql_函數僅適用於由mysql_函數生成的值/變量。 PDO是一個完全不同的API。 – deceze