[英]Retrieve javascript variable from mysql database
this variable for locations is working fine in my code. 这个位置变量在我的代码中工作正常。 http://vince.netau.net http://vince.netau.net
var locations = [
["John Doe", "145 Rock Ridge Road, Chester, NY ", "41.314926,-74.270134", "http://maps.google.com/mapfiles/ms/icons/blue.png"],
["Jim Smith", "12 Williams Rd, Montvale, NJ ", "41.041599,-74.019554", "http://maps.google.com/mapfiles/ms/icons/green.png"],
["John Jones", "689 Fern St Township of Washington, NJ ", "40.997704,-74.050598", "http://maps.google.com/mapfiles/ms/icons/yellow.png"],
];
Now my next step is instead of static data like above, I would like to retrieve data from my mysql database and use it for var locations. 现在,我的下一步是代替上面的静态数据,我想从mysql数据库中检索数据并将其用于var位置。 I have this php which is spitting out the data from mysql in exactly the same format as above. 我有这个php,它以与上述完全相同的格式从mysql吐出数据。 http://vince.netau.net/db-connect-test.php http://vince.netau.net/db-connect-test.php
<?php
$servername = "";
$username = "";
$password = "";
$dbname = "";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, name, address, lat, lng, Icon FROM markers";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '["' . $row["name"]. '"'. ', '. '"' . $row["address"].'"'.', '. '"'. $row["lat"].','. $row["lng"].'"'.', '. '"'. $row["Icon"]. '"]'. ','. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
I just don't know how to make it work. 我只是不知道如何使它工作。 Would appreciate any help on how to perform that. 将不胜感激如何执行该操作。 I am a beginner. 我是初学者。
Thanks 谢谢
Update-David, I tried the code you wrote. 更新-David,我尝试了您编写的代码。 I'm still not sure what this does for me as far as getting the location data in the html? 我仍然不确定就html中的位置数据而言,这对我有什么作用?
<?php
//open connection to mysql db
$connection = mysqli_connect("","","","") or die("Error " . mysqli_error($connection));
//fetch table rows from mysql db
$sql = "select * from markers";
$result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));
$location = array();;
if ($result->num_rows > 0) {
// output data of each row
$key = 0;
while($row = $result->fetch_assoc()) {
$locations[$key][] = $row["name"];
$locations[$key][] = $row["address"];
$locations[$key][] = $row["lat"];
$locations[$key][] = $row["lng"];
$locations[$key][] = $row["Icon"];
$key++;
}
}
else {
echo "0 results";
}
echo json_encode($locations);
//close the db connection
mysqli_close($connection);
?>
Here are the results, a little messed up. 这是结果,有点混乱。
[["John Doe","147 Rock Ridge Road, Chester, NY ","41.314926","-74.270134","http://maps.google.com/mapfiles/ms/icons/blue.png"],["Jim Smith","14 Williams Rd, Montvale, NJ ","41.041599","-74.019554","http://maps.google.com/mapfiles/ms/icons/green.png"],["John Jones","691 Fern St Township of Washington, NJ ","40.997704","-74.050598","http://maps.google.com/mapfiles/ms/icons/yellow.png"]] [[“ John Doe”,“纽约州切斯特市Rock Ridge Road 147”,“ 41.314926”,“ -74.270134”,“ http://maps.google.com/mapfiles/ms/icons/blue.png”], [“吉姆史密斯”,“新泽西州蒙特维尔市威廉斯路14号”,“ 41.041599”,“ -74.019554”,“ http://maps.google.com/mapfiles/ms/icons/green.png”],[“约翰·琼斯(John Jones)”,“新泽西州弗恩街691号,新泽西州”,“ 40.997704”,“ -74.050598”,“ http://maps.google.com/mapfiles/ms/icons/yellow.png”]]
On the server side, you should use json_encode to insert the data into the page. 在服务器端,您应该使用json_encode将数据插入页面。
eg 例如
<?php
echo "<script> var data = '" + json_encode($locations) + "'; </script>";
?>
On the client side, 在客户端,
var locations = JSON.parse(data);
I see you are try to get location array ,so store as multidimentional array will be helpful 我看到您正在尝试获取位置数组,因此将其存储为多维数组会有所帮助
$location = array();;
if ($result->num_rows > 0) {
$key = 0;
// output data of each row
while($row = $result->fetch_assoc()) {
$locations[$key][] = $row["name"];
$locations[$key][] = $row["address"];
$locations[$key][] = $row["lat"];
$locations[$key][] = $row["lng"];
$locations[$key][] = $row["Icon"];
$key++;
}
}
else {
echo "0 results";
}
//echo json_encode($locations);
<?php
echo "<script> var locations = '" . json_encode($locations) . "'; </script>";
?>
So lets assume the php file is now giving me the exact location data that was formerly hard coded under var=locations in the html. 因此,假设php文件现在为我提供了确切的位置数据,该数据以前是在html中的var = locations下硬编码的。
What is the new code I need to use in the html so that I call the php file and the hard coded data will be replaced with the php locations results which was pulled from the mysql table. 我需要在html中使用什么新代码,以便调用php文件,硬编码数据将替换为从mysql表中提取的php位置结果。
Thanks 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.