如何从数据库中检索信息并使用AJAX

时间:2017-12-21 21:07:43

标签: javascript php ajax

我有一个包含6个橄榄球结果和团队的数据库,我想使用AJAX从结果数据库中获取数据并在php页面上显示。这是一个我正在使用的页面,名为getData.php

<?php

$year=$_GET['year'];

$connection = mysqli_connect("localhost","root","");
mysqli_select_db($connection,"6nationsDB");


// for results
$result = mysqli_query($connection,"SELECT * FROM results WHERE 
year(`date`)='$year' ORDER BY rnd, date");
$rs = array();
$i=0;
while($rs[] = mysqli_fetch_assoc($result)) {
// do nothing ;-)
}

// for team details
$result2 = mysqli_query($connection,"SELECT * FROM teams ORDER BY id");
$rs2 = array();
$i=0;
while($rs2[] = mysqli_fetch_assoc($result2)) 
{

}


mysqli_close($connection);

unset($rs[count($rs)-1]);  //removes a null value
// print("{ \"results\":" . json_encode($rs) . "}");
print("{ \"results\":" . json_encode($rs) . " , \"teams\":" . 
json_encode($rs2) . "}");
?>

尝试访问getData.php中的select语句以从数据库中获取数据

Browse.php

<link rel="stylesheet" href="design.css" type="text/css">
<!DOCTYPE html>
<html>

<div id="getData"></div>

<script type ="text/javascript">
dis();
function dis()
{
    xmlhttp = new XMLHttpRequest();
    xmlhttp.open("GET",  "getData.php",false);
    xmlhttp.send(null);
    document.getElementById("getData").innerHTML=xmlhttp.responseText;

}

</script>

0 个答案:

没有答案