2013-10-18 4 views
0

앱 프로젝트 용 API를 설정하려고합니다.mysql (mysqli)에서 결과를 얻을 수 없습니다.

나는 행을 추가 한 'users'라는 mysql 테이블이있다.

코드를 사용하여 : 나는 빈 결과를 얻을 수

// Create connection 
$mysqli = new mysqli("localhost","user", "pass", "db"); 

// Check connection 
if($mysqli->connect_errno){ 

    $result = "Failed to connect to MySQL: " . mysqli_connect_error(); 
    print_r(json_encode($result));  
    return false; 

} 

$row = $mysqli->query("SELECT * FROM users"); 
print_r(json_encode($row)); 

을, 어떻게?

내가 얻을 정확하기 위하여 (연결 오류가 발생하지 않습니다) :

{"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null} 

편집 :

이 덕분에 원래의 질문에 YM에 대한 답을 얻었다!

{"0":"test","username":"test","1":"[email protected]","email":"[email protected]","2":"test","password":"test","3":"2013-10-18 22:22:53","date_registered":"2013-10-18 22:22:53","4":"1","id":"1"} 

같은 내가 원하는됩니다 :

{"username":"test","password":"test","email":"[email protected]", ...etc } 

내가 그것을 얻는 어떻게 결과를 얻을

$row = $mysqli->query("SELECT * FROM users WHERE email = '".$email."'"); 
$result = $row->fetch_array(); 
print_r(json_encode($result)); 

:

는 이제 코드를 사용하고 계십니까?

+0

이전에 mysql_ *을 사용했다면 ... mysql_query()를 실행하면 결과를 얻을 수 있습니까? 나는 그렇게 생각하지 않는다. 쿼리에서 배열 가져 오기 –

+2

$ row에서 결과를 가져올 필요가 없습니까? –

+0

내 대답을 수정했습니다. 숫자 인덱스로 결과가 중복되지 않게하려면 [mysqli_fetch_assoc()] (http://www.php.net/manual/en/mysqli-result.fetch-assoc.php)를 사용해야합니다. –

답변

1

mysqli_query()$rows = $result->fetch_all(MYSQLI_ASSOC);mysqli_result를 반환합니다, 당신은 귀하의 행을하기 전에 행을 fetch (as an array in this case)해야합니다. 그들과 어울려. 추가 된 라인 :

// Create connection 
$mysqli = new mysqli("localhost","user", "pass", "db"); 

// Check connection 
if($mysqli->connect_errno){ 

    $result = "Failed to connect to MySQL: " . mysqli_connect_error(); 
    print_r(json_encode($result));  
    return false; 

} 

// Get a mysql_result 
$row = $mysqli->query("SELECT * FROM users"); 

// Get it into an array without numeric indexes 
$result = $row->fetch_assoc(); 

// Display the row 
print_r(json_encode($result)); 
2

이 시도 :

$result = $mysqli->query("SELECT * FROM users"); 
$row = $result->fetch_array(MYSQLI_ASSOC); 
print json_encode($row); // json_encode returns a string... 

이 연관 배열이 시도 :

while($row = $result->fetch_array(MYSQLI_ASSOC)) 
{ 
    $rows[] = $row; 
} 
print json_encode($rows); 

또는 당신이 시도 할 수 있습니다 ...

관련 문제