我想从 PHP 检索一组数组到 ajax。我的代码没有返回任何东西,有人可以帮助我如何将值从 PHP 检索到 ajax。如果我不在 mysql_fetch 中创建数组,它将从数据库中检索最后一个值并将其传递给 ajax 函数。我想获得全部价值。我怎样才能做到这一点?
阿贾克斯代码:
<script>
//Global Variables
var channel;
function overall(){
$(".one").show();
$(".two").hide();
$(".three").hide();
$(".four").hide();
window['channel']="overall";
$.ajax({
type:"GET",
url:"dash2.php",
data:{channel:channel},
dataType:'json',
success:function(data){
console.log(data.a);
console.log(data.b);
console.log(data.c);
}
});
}
</script>
PHP代码:
<?php
error_reporting(0);
$channel=$_GET['channel'];
$host="192.168.0.29";
$username="root";
$password="root";
$dbname="realcl";
mysql_connect($host,$username,$password)
OR DIE ('Unable to connect to database! Please try again later.');
mysql_select_db($dbname);
$query = 'select * from '.$channel;
$masterresult = mysql_query($query);
$success[];
$timeout[];
$fail[];
while($row1 = mysql_fetch_array($masterresult))
{
$success[]=$row1[1];
$timeout[]=$row1[2];
$fail[]=$row1[3];
}
echo json_encode(array("a"=>$success,"b"=>$timeout,"c"=>$fail));
?>