【问题标题】:Retrieve javascript variable from mysql database从 mysql 数据库中检索 javascript 变量
【发布时间】:2016-06-02 12:19:28
【问题描述】:

这个位置变量在我的代码中运行良好。 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"],
 ];

现在我的下一步是代替上面的静态数据,我想从我的 mysql 数据库中检索数据并将其用于 var 位置。我有这个 php,它以与上面完全相同的格式从 mysql 中吐出数据。 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();
?> 

我只是不知道如何使它工作。将不胜感激有关如何执行此操作的任何帮助。我是初学者。

谢谢

Update-David,我试过你写的代码。就在 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);
?>

这是结果,有点乱。

[["John Doe","147 Rock Ridge Road, Chester, NY","41.314926","-74.270134","http://maps.google.com/mapfiles/ms/icons/blue.png "],["吉姆·史密斯","14 Williams Rd, Montvale, NJ","41.041599","-74.019554","http://maps.google.com/mapfiles/ms/icons/green.png"] ,["约翰·琼斯","新泽西州华盛顿市弗恩街 691 号","40.997704","-74.050598","http://maps.google.com/mapfiles/ms/icons/yellow.png"]]

【问题讨论】:

  • 查看json_encode

标签: javascript php mysql


【解决方案1】:

我看到你正在尝试获取位置数组,所以存储为多维数组会有所帮助

$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>";
?>

【讨论】:

  • 嗨大卫,我应该把你写的代码放在哪里,除了它我还需要什么来完成这项工作?抱歉,知识太少了。
  • 尝试 var_dump($locations); !它是你想要的 javascript 位置类型吗
  • 目前,我在 html 中有位置数据。我希望位置数据来自 mysql 数据库。我只是不知道要写的代码,所以它会从 mysql 数据库中提取数据。
  • 是的,它确实有名字。此回显将位置数据按照我需要的确切顺序放置,就像 html 中的位置数据一样。 echo '["' . $row["name"]. '"'. ','。 '"' . $row["address"].'"'.', '. '"'.$row["lat"].','.$row["lng"].'"'.','. '"'.$row["Icon"].'"]'. ','。 "
    ";
  • PHP 数组不是多维的,您可以用 while ($row=$result-&gt;fetch_array(MYSQLI_NUM)) $locations[]=$row; 替换循环,但除此之外是一个很好的答案
【解决方案2】:

在服务器端,您应该使用 json_encode 将数据插入到页面中。

例如

<?php
echo "<script> var data = '" + json_encode($locations) + "'; </script>";
?>

在客户端,

var locations = JSON.parse(data);

【讨论】:

  • 对不起,我是新手。那是如何从 Mysql 中获取行数据的呢?
【解决方案3】:

所以让我们假设 php 文件现在为我提供了以前在 html 中的 var=locations 下硬编码的确切位置数据。

我需要在 html 中使用什么新代码,以便我调用 php 文件,硬编码数据将替换为从 mysql 表中提取的 php 位置结果。

谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-19
    • 1970-01-01
    • 2017-08-02
    • 2013-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多