2016-11-14 91 views
3

我有這樣的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"; 
+0

Replcae這兩條線:$查詢= $ conn->準備(「選擇*從圖像WHERE image_caption LIKE%」。$ _ POST ['search']。「%」); // $ query-> bind_param('s',$ likeString); –

+0

錯誤:'調用成員函數execute()在非對象' – Jason

+0

var * $ res *未在您的所有'$ res-> num_rows> 0中定義* is * false * – varlogtim

回答

4

試試這個:(代碼更新)

$likeString= "%{$_POST['search']}%"; 
$stmt = $db->prepare("SELECT * FROM images WHERE image_caption LIKE ?"); 
$stmt->bind_param('s', $likeString); 
$stmt->execute(); 

$result = $stmt->get_result(); 
    while ($row = $result->fetch_array(MYSQLI_NUM)) 
    { 
     foreach ($row as $r) 
     { 
      echo "<br>Title: " . $r['image_caption'];     
     } 
     print "\n"; 
    } 

OR

<?php 
    $conn = new mysqli("localhost","mydbUsername","myPass","myDbName"); 
    /* check connection */ 
    if ($conn->connect_errno) { 
    printf("Connect failed: %s\n", $conn->connect_error); 
    exit(); 
    } 

    $query = "SELECT * FROM images WHERE image_caption LIKE %".$_POST['search']."%"; 

    if ($result = $conn->query($query)) { 

    /* fetch associative array */ 
    while ($row = $result->fetch_assoc()) { 
     echo "<br> Title: ". $row["image_caption"]); 
    }print "\n"; 

    /* free result set */ 
    $result->free(); 
    } 

    /* close connection */ 
    $conn->close(); 
?> 
+0

它應該是這個'$ param'而不是'$ likeString'。 –

+0

有什麼區別?只有'$ _POST ['search']'上的括號?仍然沒有結果.. – Jason

+0

嘗試與上述更新,也許有幫助(不太確定) –

5

您正在使用$res而沒有定義它...您必須改用$query。錯誤報告 下一次輪到看到這樣愚蠢的錯誤

+0

仍然是'0結果'。我在頂部添加了'error_reporting(E_ALL);'和'error_reporting(0);'..無錯誤 – Jason

+0

error_reporting(0)塊顯示錯誤。您必須設置E_ALL和ini_set('display_errors','開') – nospor

+0

您沒有得到0結果。你有一個條件爲false的if語句,因爲你使用了一個沒有值的變量'$ res',然後你迴應你有0個結果。 – varlogtim