2010-07-20 3 views
2

가능한 중복 :
mysql_fetch_array() expects parameter 1 to be resource, boolean given in selectPHP & MySQL은 - 경고 : mysqli_num_rows는() 매개 변수 1 mysqli_result 수, 부울 주어진 기대

나는 점점 keep0 오류를 수정하는 방법을 궁금

아래에 열거되어 있습니다.

다음과 같은 오류가 발생합니다.

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

라인 (159)은 여기

$foundnum = mysqli_num_rows($run); 

& 인 PHP의 MySQL 코드의 일부분이다.

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

가 FALSE 반환하기 때문에

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

.. –

답변

2

그것은이다. 결과 집합으로 사용하기 전에 반환 값을 테스트해야합니다. 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' ; 
} 

당신 안에 당신을 넣어 'OR', 오류가 어쨌든 arount가 의심 그래서처럼 $ 접두사 변수를 사용하여 루프에서 복잡한 SQL 문자열을 반복하지 않도록하려면 바깥 대신에 괄호.

+0

내가 반환 값을 테스트하려면 어떻게합니까? – lone

+0

내 댓글에 수정 사항보기 –

3

이 false를 반환하지 않도록 $run을 테스트해야합니다.

if ($run != false) { 
    $foundnum = mysqli_num_rows($run); 
    if ($foundnum == 0) { 
    echo 'No results found.'; 
    } 
} 
당신은하지 이미 수행하는 경우, SQL 문에서 그들을 삽입하기 전에이 $의 search_each 문자열에는 mysql_real_escape_string()를 사용 할 수 있습니다
관련 문제