2010-07-20 262 views
2

可能重複:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in selectPHP和MySQL - 警告:mysqli_num_rows()預計參數1被mysqli_result,布爾給

我不知道如何可以糾正我keep0得到錯誤下面列出。

我收到以下錯誤消息。 159

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given on line 159 

線是

$foundnum = mysqli_num_rows($run); 

這裏是PHP & MySQL的代碼的一部分。

mysqli_select_db($mysqli, "sitename"); 

     $search_explode = explode(" ", $search); 

     foreach($search_explode as $search_each) { 
      $x++; 
      if($x == 1){ 
       $construct .= "(users_comments.article_content LIKE '%$search_each%' OR users_comments.title LIKE '%$search_each%' OR users_comments.summary LIKE '%$search_each%')"; 
      } else { 
       $construct .= "(OR users_comments.article_content LIKE '%$search_each%' OR users_comments.title LIKE '%$search_each%' OR users_comments.summary LIKE '%$search_each%')"; 
      } 

     } 

     $construct = "SELECT users.*, users_comments.* FROM users INNER JOIN users_comments ON users.user_id = users_comments.user_id WHERE $construct ORDER BY users_comments.date_created DESC"; 
     $run = mysqli_query($mysqli, $construct); 

     $foundnum = mysqli_num_rows($run); 

    if ($foundnum == 0) { 
     echo 'No results found.'; 
    } 
+1

您可能需要使用mysql_real_escape_string()這些$ search_each字符串在SQL語句中嵌入它們之前,如果尚未.. – 2010-07-20 18:32:13

回答

2

這是因爲

if ($result = mysqli_query($mysqli, $construct)) { 
    // got something! 
    } 

返回FALSE。在將其用作結果集之前,您必須測試返回值。您的SQL語句中必須有錯誤。

小編建議:爲了避免你的循環重複複雜的SQL字符串,使用$前綴的變量,就像這樣:

$prefix = ''; 
foreach($search_explode as $search_each) { 
    $construct .= "$prefix (users_comments.article_content LIKE '%$search_each%' OR users_comments.title LIKE '%$search_each%' OR users_comments.summary LIKE '%$search_each%') "; 
    $prefix = ' OR' ; 
} 

我懷疑的錯誤是arount有反正,你把你的「或」內括號而不是外部。

+0

如何測試的返回值? – lone 2010-07-20 18:25:21

+0

請參閱我的評論中的修改 – 2010-07-20 18:29:51

3

您必須測試$run以確保mysqli_query()沒有返回false。

if ($run != false) { 
    $foundnum = mysqli_num_rows($run); 
    if ($foundnum == 0) { 
    echo 'No results found.'; 
    } 
} 
相關問題