https://developers.google.com/maps/documentation/javascript/mysql-to-maps
不推荐使用 PHP 连接到 MySQL 数据库的功能,即使用 mysql 而不是 mysqli 或 pdo。
我尝试按照教程显示来自 mySQL 数据库的具有设置名称/纬度/经度的标记,但发现 PHP 已被弃用,只需将“mysql”替换为“mysqli”就得到了一个几乎空的 XML 文档。
这是我的 PHP:
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "pins_db";
$database = "root-pins_db";
function parseToXML($htmlStr)
{
$xmlStr=str_replace('<','<',$htmlStr);
$xmlStr=str_replace('>','>',$xmlStr);
$xmlStr=str_replace('"','"',$xmlStr);
$xmlStr=str_replace("'",''',$xmlStr);
$xmlStr=str_replace("&",'&',$xmlStr);
return $xmlStr;
}
// Opens a connection to a MySQL server
$connection=mysqli_connect ('localhost', $username, $password);
// Set the active MySQL database
$db_selected = mysqli_select_db($connection, $dbname);
// Select all the rows in the markers table
$query = "SELECT * FROM pins_table WHERE 1";
$result = mysqli_query($query);
header("Content-type: text/xml");
// Start XML file, echo parent node
echo '<markers>';
// Iterate through the rows, printing XML nodes for each
while ($row = @mysqli_fetch_assoc($result)){
// Add to XML document node
echo '<marker ';
echo 'name="' . $row['name'] . '" ';
echo 'lat="' . $row['lat'] . '" ';
echo 'lng="' . $row['lng'] . '" ';
echo '/>';
}
// End XML file
echo '</markers>';
?>
生成的 XML 文档仅包含:
<markers/>
我在 MAMP 上运行 PHP 7.0.15 和 MySQL 5.6.35。
数据库信息:
user/pw: root/root
database: pins_db
table: pins_table
表格:
1 id int(11)
2 datetime datetime
3 lat float(10,6)
4 lng float(10,6)
5 name text
我是不是漏掉了什么?我对 PHP 很陌生,对 mySQL 也很陌生;我在网上其他任何地方都找不到这个问题的答案。
最佳答案
开启php错误报告
error_reporting(E_ALL);
ini_set('display_errors', 1);
http://php.net/manual/en/function.error-reporting.php
http://php.net/manual/en/function.ini-set.php
测试查询执行是否成功。
$result = mysqli_query($connection,$query);
if(!$result) {
// error returned
die('error#002: '.mysqli_error($connection));
}
http://php.net/manual/en/mysqli.error.php
删除函数调用前的 @
(at 符号),因为我们不想消除 php 错误:
while ( $row = @mysqli_fetch_assoc($result) ) {
^
同时检查连接是否成功
$connection = mysqli_connect('localhost', $username, $password);
if(!$connection) {
die('error connecting: ' . mysqli_connect_error());
}
http://php.net/manual/en/mysqli.connect-error.php
也有可能 mysqli_select_db
没有工作。例如,如果数据库名称不正确或不存在。
http://php.net/manual/en/mysqli.select-db.php
请注意,数据库名称可以作为 mysqli_connect
的第四个参数提供。
https://ericlippert.com/2014/03/05/how-to-debug-small-programs/
关于php - Google map 文档使用已弃用的 PHP - 我该如何更新它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42537769/