2013-07-23 2 views
-1

나는이 오류를 얻고있다 주어진 기대 여기 경고 : 코드 "searchcar.php"에서 "배열에서 주어진 매개 변수 (1) mysqli_result 수() 기대 mysqli_fetch_assoc 경고"mysqli_fetch_assoc는()는 매개 변수 1 mysqli_result 수, 배열

$modelmake = $_POST['model_make']; 
$result = $db->select('car_information','*', 'Model_Make LIKE \'%$modelmake%\''); 
while($row = mysqli_fetch_assoc($result)) 
{ 
    echo 'Model'.$row['model_make']; 
} 

는 "database.php"선택 기능에서

public function select(
     $table, 
     $fields = '*', 
     $where = '1=1', 
     $order = '', 
     $limit = '', 
     $desc = false, 
     $limitBegin = 0, 
     $groupby = null, 
     $monitoring = false 
    ) //monitoring is set to true to view the actual query 
    { 
// $query ='SELECT ' . $fields . ' FROM ' . $table ; 
    $query = 'SELECT ' . $fields . ' FROM ' . $table . ' WHERE ' . $where; 

     if (!empty($groupby)) { 
      $query .= ' GROUP BY ' . $groupby; 
     } 

     if (!empty($order)) 
     { 
      $query .= ' ORDER BY ' . $order; 

      if ($desc) 
      { 
       $query .= ' DESC'; 
      } 
     } 

     if (!empty($limit)) 
     { 
      $query .= ' LIMIT ' . $limitBegin . ', ' . $limit; 
     } 

     $result = $this->_sendQuery($query); 

     $resultArray = array(); 

     while ($row = mysqli_fetch_assoc($result)) 
     { 
      $resultArray[] = $row; 
     } 

     if ($monitoring) 
     {  
      // If monitoring is activated, echo the query 
      echo $query; 
     } 
     return $resultArray; 
    }  

나는이 줄을 사용하려면 "동안 ($ 행 = mysqli_fetch_assoc ($ 결과))" 제발 조언을 코드입니다!

+0

무엇이 var_dump ($ result)를 출력합니까? –

+0

'select()'콜백은 이미 mysqli_fetch_assoc을 사용하여 배열을 수집합니다. 나중에 다시 호출해야한다고 생각하는 이유는 무엇입니까? – mario

답변

0

사용 mysqli_error() http://www.php.net/manual/en/mysqli.errno.php

mysqli_fetch_array()/mysqli_fetch_assoc()/mysqli_fetch_row() expects parameter 1 to be resource or mysqli_result, boolean given

MySQL의 명령 줄이나 도구에서 쿼리를 실행하여 최근 MySQL 작업으로 발생한 에러 메시지를 반환 phpMyAdmin과 같습니다. 쿼리에 구문 오류가 있으면 이 무엇인지 알려줍니다.

따옴표가 정확한지 확인하십시오. 쿼리 주위에 누락 된 견적 또는 값이 있으면 쿼리가 실패 할 수 있습니다.

값을 이스케이프하고 있는지 확인하십시오. 검색어에 따옴표가 있으면 검색어가 실패 할 수 있으며 SQL 주입을 계속 열어 둘 수 있습니다. 입력을 이스케이프하려면 mysql_real_escape_string()을 사용하십시오.

+0

var_dump ($ result)는 배열 (0) {}을 반환합니다 이 상황을 해결하는 방법보다 "마리오"나는 프로그래밍을 참새에 초보자입니다 – Hassan

+0

감사합니다 마이크와 모두 내 문제가 해결되었습니다. – Hassan

5

select 메서드는 resource이 아닌 array을 반환합니다. 즉, mysqli_fetch_assoc은 불평 할 권리가 있습니다.

좋은 소식은 select 방법을 사용 select() 방법은 명확하게 쿼리를 수행

foreach($result as $row) 
1

두고 while($row = mysqli_fetch_assoc($result))을 대체 할 수 있음을 의미하고 결과의 배열을 반환하는 결과의 배열을 반환하는 것입니다. 왜 그 어레이를 취할 수있을 것이라고 기대할 수 있습니까? (다시 말하면 mysqli_fetch_assoc()을 다시 할 수 있습니까?

나는 그냥 직접이 같은 $result 반복 할 수 있다고 생각 :

$modelmake = $_POST['model_make']; 
$result = $db->select('car_information','*', 'Model_Make LIKE \'%$modelmake%\''); 
foreach ($result as $row) 
    echo 'Model'.$row['model_make']; 
} 

또한 코드가 SQL 인젝션에 취약 지적한다.

관련 문제