目前,我制作了脚本,在 onclick 事件之后,将问题发送到数据库并在 console.log 中显示数据(来自数组)。这一切都工作正常,但是..我想在代码中的不同位置显示数组中的数据。当我尝试使用 DataType 'json' 然后显示一些数据时,它在我的 console.log 中什么也不显示。所以,我的问题是:如何解决显示数据的问题?如您所见,这是一个好主意吗?
下面您可以看到我当前的代码:
$(document).ready(function(){
$(".profile").click(function(){
var id = $(this).data('id');
//console.log(id);
$.ajax({
method: "GET",
url: "../functions/getDataFromDB.php",
dataType: "text",
data: {id:id},
success: function(data){
console.log(data);
}
});
});
});
:
public function GetPlayer($id){
$id = $_GET['id'];
$query = "SELECT name,surname FROM zawodnik WHERE id='".$id."'";
$result = $this->db->query($query);
if ($result->num_rows>0) {
while($row = $result->fetch_assoc()){
$this->PlayerInfo[] = $row;
}
return $this->PlayerInfo;
}else {
return false;
}
}
:
$info = array();
$id = $_GET['id'];
$vv = new AddService();
foreach($vv->GetPlayer($id) as $data){
$info[0] = $data['name'];
$info[1] = $data['surname'];
}
echo json_encode($info);
最佳答案
我认为将 mysqli 中的 fetch_all 行更改为 rm -rf 会更好。数据库中的信息全部过时,或者完全不真实。
关于php - 使用ajax、mysql、php显示数据库中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49212565/