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('<','<',$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;我在网上其他任何地方都找不到答案.
解决方法:
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/