2014-07-16 5 views
0

PHP 스크립트를 사용하여 데이터베이스를 쿼리하고 JSON 문자열을 전달하려고합니다. 나는이 스크립트를 jQuery Ajax에서 사용하고있다. 내가 JSON 문자열처럼되고 싶어 1, 2결과를 가져올 때 스크립트가 null을 반환합니다.

- -,

아이디 B

A -

이름 { "A":

<?php 

$con = mysqli_connect("localhost","root","password","test") or die("Some error occurred during connection " . mysqli_error($con));   
$strSQL = "SELECT name,id from build";   
$query = mysqli_query($con, $strSQL); 
$builder_json=array(); 
$row_array=array();  
while($result = mysqli_fetch_array($query)) 
{ 
    // $builder_json[]=$result['name'].":".$result['id'];   
    $row_array['name'] = $result['name']; 
    $row_array['id'] = $result['id']; 
    array_push($builder_json[],$row_array); 
} 
echo json_encode($builder_json); 
mysqli_close($con); 

?> 

지금 가져온 데이터가 같은 경우 : "1", "B": "2"}}; 위 코드를 사용하면 NULL이됩니다. 어떻게 내가

+0

array_push ($ builder_json [], $ row_array)에서'[]'를 제거하십시오. –

답변

1

필요하지 않은 일을 한 실수 인 [] 당신의 array_push 호출 그렇지 않으면

//array_push($builder_json[],$row_array); 
array_push($builder_json,$row_array); 

는 다음과 같은 오류 제공에 변수 후 :

경고 : array_push()는 매개 변수 1을 배열로 간주하고, null을 줄 수 있습니다.

관련 문제