2016-04-19 5 views
-1

私はdbから同じ名前を持つことができるユーザーのリストを取得する必要があります。私は問題を私の質問の結果をjsonに変換している。複数のjsonをPHP経由で返す

私が1つの結果しか取得しようとしなかった場合、コードは機能しますが、LIKEの使用が開始されてから失敗しました。

PHPコード:

<?php 


$q = $_POST['q']; 

require('connect.php'); 

$i = 0; 

$sql="SELECT `_nome`, `_endereco`, `_telefone`, `_imgstring`, `_dtAcesso`, `_descricao`, `_fkIdUser` FROM `tbvisitantes` WHERE _nome LIKE CONCAT ('%',?,'%')"; 
$stmt= $conn->prepare($sql); 
$stmt->bind_param('s', $q); 


if ($stmt){ 
$stmt->execute(); 

$stmt->bind_result($rName, $rEndereco, $rTelefone, $rImgString, $rDtAcesso, $rDescricao, $rFkIdUser); 

while ($row = $stmt->fetch_array()){ 

$json_output = array('name' => $rName, 'address' => $rEndereco, 'tel' => $rTelefone, 'imgString' => $rImgString, 'dtAcesso' => $rDtAcesso, 'descricao' => $rDescricao, 'fkIdUser' => $rFkIdUser); 

echo json_encode($json_output); 

} 

$stmt->close(); 

} 
mysqli_close($conn); 

?> 

結果を受信するスクリプト:

$.ajax({ 
url: 'getuser.php', 
type: 'POST', 
data: {q: str}, 
success: function(jsonString){ 

$vIndex = '<input class="vInput"></input>'; 

$($cadContent).appendTo('#cadContent'); 

var json = $.parseJSON(jsonString); 

var jsonLenght = Object.keys(jsonString).length; 


$.each(json, function(index, el) { 

$($vIndex).attr('id', index).val(el).appendTo('#txtHint'); 


}); 


} 
}) 
.done(function() { 
console.log("success"); 
}) 
.fail(function(xhr) { 

alert("An error ocurred:" + xhr.status + xhr.statusText); 

console.log('failed'); 
}) 
.always(function() { 
console.log("complete"); 
}); 


} 

私は私の問題は私のJSONの構造にすることによって可能性があることと思いますが、どのように私は今ありません修理する。

答えて

0

あなたはJSONで複数の応答をエコーすることはできません。応答を1つの配列に結合し、応答をエコーする必要があります。

これ以下のスニペットますが、参考になりまし

Serverページ:Ajaxのページで

while ($row = $stmt->fetch_array()){ 

    $json_output[] = array('name' => $rName, 'address' => $rEndereco, 'tel' => $rTelefone, 'imgString' => $rImgString, 'dtAcesso' => $rDtAcesso, 'descricao' => $rDescricao, 'fkIdUser' => $rFkIdUser); 

} 
echo json_encode($json_output); 

// your success callback handler 
function handler(response) { 
    // execute code for object 1 
    doStuff(response[0]); 
    // execute code for object 2 
    doOtherStuff(response[1]); 
} 
0

あなたは、単一のJSON配列に行を結合する必要があります。

if ($stmt){ 
    $stmt->execute(); 

    $stmt->bind_result($rName, $rEndereco, $rTelefone, $rImgString, $rDtAcesso, $rDescricao, $rFkIdUser); 
    $rows = array(); 

    while ($row = $stmt->fetch_array()){ 

    $json_output = array('name' => $rName, 'address' => $rEndereco, 'tel' => $rTelefone, 'imgString' => $rImgString, 'dtAcesso' => $rDtAcesso, 'descricao' => $rDescricao, 'fkIdUser' => $rFkIdUser); 
    $rows[] = $json_output; 
    } 
    echo json_encode($rows); 
} 
関連する問題