我正在使用 mysql 查询从 db 获取数据。这些数据将通过以下方式在 js 函数中使用:
数据库查询页面:
<?php
$server = "localhost"; // MySql host name
$username = "root"; // MySQL username
$password = ""; // MySQL password
$dbname = "admins"; // MySQL database name
$table = "usersvisit"; // MySql table name
$db=mysql_connect($server,$username,$password) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());
$results=mysql_query("SELECT * FROM $table LIMIT 30") or die(mysql_error());
$data="[";
while($row=mysql_fetch_assoc($results))
{
$data.= "'".$row['idUser']."',";
}
$data.="];";
echo $data;
?>
然后在 HTML 页面中,我想将检索到的数据声明为一个名为 origins 的变量,因此 var origins 必须存储 $data 以便稍后在其他 js 函数中使用它:
function createRequest()
{
var xmlhttp = false;
try {xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");}catch(e){try {xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");}catch(E){xmlhttp = false;}}
if(!xmlhttp && typeof XMLHttpRequest!='undefined'){try{xmlhttp = new XMLHttpRequest();}catch(e){xmlhttp=false;}}
if(!xmlhttp && window.createRequest){try{xmlhttp = window.createRequest();}catch(e){xmlhttp=false;}}
return xmlhttp;
}
window.onload=createRequest;
// Make an xmlHttpRequest to query_database.php
function request_database_query()
{
var url = "query_database.php";
var con=createRequest();
con.open("GET",url,true);
con.onreadystatechange=function()
{
if(con.readyState==4 && con.status==200)
{
document.getElementById("div2").innerHTML=con.responseText;
}
}
con.send(null);
}
window.onload=request_database_query;
var origins= ... // in this var i want to store the data to be as the followling
/*var origins = [
'Euston',
'Kings Cross',
'Liverpool St',
'Paddington',
'St. Pancras',
'Victoria',
'Waterloo'
];*/
有什么建议么?