微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Google Maps文档使用了已弃用的PHP-如何更新它?

https://developers.google.com/maps/documentation/javascript/mysql-to-maps

不推荐使用PHP功能来连接到MysqL数据库,即使用MysqL代替MysqLi或pdo.

我尝试按照本教程显示来自MysqL数据库的具有设置名称/ lat / lng的标记,但是发现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('<','&lt;',$htmlStr);
$xmlStr=str_replace('>','&gt;',$xmlStr);
$xmlStr=str_replace('"','&quot;',$xmlStr);
$xmlStr=str_replace("'",'&#39;',$xmlStr);
$xmlStr=str_replace("&",'&amp;',$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

函数调用之前删除@(在符号处),因为我们不想让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/

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐