我有這樣的PHP代碼簡單搜索函數返回總是0結果
if(isset($_POST['submit'])){
$likeString = '%' . $_POST['search'] . '%';
$query = $conn->prepare("SELECT * FROM images WHERE image_caption LIKE ?");
$query->bind_param('s', $likeString);
$query->execute();
var_dump($likeString);
if (!$query) {
printf("Query failed: %s\n", $mysqli->error);
exit;
}
if($res->num_rows > 0) {
while ($row = $res->fetch_assoc()) {
echo "<br>Title: " . $row['image_caption'];
}
} else {
echo " <br> 0 results";
}
}
var_dump($likeString)
顯示我已經通過搜索表單提交正確的字。我也試過在phpmyadmin直接運行這個查詢
SELECT * FROM images WHERE image_caption LIKE "%Volvo%"
而且我收到1個結果是正確的。在頁面上,我看到0 results
。試圖用取玩:
$res->fetch_assoc()
$res->fetchAll()
$res->fetch()
他們沒有表現出任何結果。我相信是非常愚蠢和簡單的錯誤,但看不到它。請幫助。
我沒有Call to a member function bind_param() on a non-object
這是我的錯誤,而我從一個答案中提出了修改。問題仍然存在 - 0結果
UPDATE:當前代碼
$likeString = "%{$_POST['search']}%";
$query = $conn->prepare("SELECT * FROM images WHERE image_caption LIKE ? ");
$query->bind_param('s', $likeString);
$query->execute();
if($query->num_rows > 0) {
while ($row = $query->fetch()) {
echo "<br>Title: " . $row['image_caption'];
}
} else {
echo " <br> 0 results";
}
}
更新2:DB連接checked->結果是Connected successfully
$servername = "localhost";
$username = "mydbUsername"; // it's changed for the question
$password = "myPass"; // it's changed for the question
$dbname = "myDbName"; // it's changed for the question
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
Replcae這兩條線:$查詢= $ conn->準備(「選擇*從圖像WHERE image_caption LIKE%」。$ _ POST ['search']。「%」); // $ query-> bind_param('s',$ likeString); –
錯誤:'調用成員函數execute()在非對象' – Jason
var * $ res *未在您的所有'$ res-> num_rows> 0中定義* is * false * – varlogtim