我有一个代码来查询我的数据库,并取回响应并将它们放在我的html上的字段中。我想通过 JSON 发送响应,然后使用该信息创建字段。但是当我回应我的json_encode($json_output)
时,我只得到了一个[true]
.
如果有人能解释为什么我会不胜感激。
Javascritp 代码:
$($cadView).find('#cadForm').on('submit', function(e){
e.preventDefault();
var str = $('#srBar').val();
if(str == ""){
alert("Campo de busca vazio.");
}else{
$.post($('#cadForm').attr('action'), {q : str}, function(data){
alert(data);
});
}
PHP代码:
<?php
$q = $_POST['q'];
require('connect.php');
$i = 0;
$sql="SELECT `_nome`, `_endereco`, `_telefone`, `_imgstring`, `_dtAcesso`, `_descricao`, `_fkIdUser` FROM `tbvisitante` WHERE _nome = ?";
$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()){
$json_output[] = $row;
echo json_encode($json_output);
}
}
mysqli_close($conn);
?>
http://php.net/manual/en/mysqli-stmt.fetch.php
mysqli_stmt::fetch 返回 null 或布尔值。 您将结果绑定到变量,这些是您应该使用的变量。
否则,您可能会查看get_result而不是获取。