2016-09-27 2 views
-1

누구든지이 일을 도와 줄 수 있습니다. 테이블에서 모든 데이터를 가져 오려고합니다. 하지만 그것은 부울로 반환 유지합니다. 부울이 주어진 이유는 무엇입니까

<?php 
    $con = mysqli_connect("localhost", "root", "", "student"); 

    $query = "SELECT * FROM `announcement` ORDER BY `announce_id` DESC"; 

    $result = mysqli_query($con, $query); 

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

    echo json_encode($data); 
?> 

주어진 오류

했다 :

> Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, 
> boolean given in D:\Xampp\htdocs\student\announcement.php on line 8 
> 
> Notice: Undefined variable: data in 
> D:\Xampp\htdocs\student\announcement.php on line 12 null 
+0

항상 ($ con, $ query) 또는 die ($ con-> error)' – Ghost

+2

쿼리가 실패하면 부울 false를 반환했습니다. – nogad

+0

[mysqli \ _fetch \ _array()/mysqli \ _fetch \ _assoc()/mysqli \ _fetch \ _row() 매개 변수 1 리소스 또는 mysqli \ _result, 부울 주어진] 기대 (http://stackoverflow.com/questions/2973202/mysqli-fetch-array-mysqli-fetch-assoc -mysqli-fetch-row-expects-parameter-1) –

답변

0

mysqli_query가 false를 반환하고 $ 결과에 bool (false)이 포함되어 있고 mysqli_fetch_assoc에 mysqli_result 객체가 필요하며 대신 bool을 지정하면 불만을 나타냅니다. 우리는 왜 mysqli_query가 false를 반환했는지 알지 못하며 오류보고가 활성화되어 있지 않기 때문에 PHP는 이유를 알려주지 않습니다. 코드에서 오류를 검사하여 이전에이 코드를 catch하고 PHP가 잘못된 것을 알려줍니다. 가장 쉬운 방법은 그렇게 : 나는이

function exception_error_handler($severity, $message, $file, $line) { 
    if (!(error_reporting() & $severity)) { 
     // This error code is not included in error_reporting 
     return; 
    } 
    throw new ErrorException($message, 0, $severity, $file, $line); 
} 
set_error_handler("exception_error_handler"); 

이제 PHP는 자세한 보고서를 제공해야 할 때마다 오류와 같은 예외 오류를 변환 오류 처리기를 실행하는 것이 좋습니다

<?php 
error_reporting(E_ALL); 
if(!mysqli_report(MYSQLI_REPORT_ALL)){ 
throw new RuntimeException("unable to set mysqli_report to MYSQLI_REPORT_ALL !"); 
} 

하여 스크립트를 시작합니다 mysqli_query가 false를 반환 한 이유를 포함하여 발생했습니다.

0

당신이 "발표"테이블에 데이터가 있는지 확인하십시오.

mysqli_num_rows() 함수를 사용하여 결과 집합에서 반환되는 행 수를 확인할 수 있습니다.

이 시도 :

$con = mysqli_connect("localhost", "root", "", "student"); 
$query = "SELECT * FROM `announcement` ORDER BY `announce_id` DESC"; 
$result = mysqli_query($con, $query); 

$rowcount = mysqli_num_rows($result); //returns number of rows 

if($rowcount > 0) { 
    while($row = mysqli_fetch_assoc($result)){ 
    $data[] = $row; 
    } 
    echo json_encode($data); 
} 

우리는 동일한에 대한 질의/관심 까봐 알려주십시오.

+0

그의 문제는 쿼리 오류 또는 연결 오류입니다. 빈 테이블이 아닙니다. –

관련 문제