我正在使用google maps api 中的本教程从 mysql 生成 xml
这是我得到的错误:
警告:
mysql_fetch_assoc()
期望参数 1 是资源
从我的代码中可以看出,$result
应该通过查询获得资源。
有谁知道它为什么会给出这个特定的错误?
编码:
<?php
// Start XML file, create parent node
$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);
?>
<?php
// connect
$host = "localhost";
$username = 'root';
$psswrd = 'root';
$db = 'sql_maps';
$dbc = mysqli_connect($host, $username, $psswrd, $db);
if(!mysqli_connect_errno() ) {
echo 'sueccess'; } else {
die("Database failed: " .
mysqli_connect_error() .
" ( " . mysqli_connect_errno() . " )"
);
}
?>
<?php
// Using PHP's domxml Functions to Output XML
// Select all the rows in the markers table
// get data
$query = "SELECT * FROM markers WHERE 1";
// catch resource(collection of database rows)
$result = mysqli_query($dbc, $query);
// check
if($result) {
echo 'success';
} else {
die("connection failed");
}
header("Content-type: application/xml");
while ($row = mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
$node = $dom->createElement("marker");
$newnode = $parnode->appendChild($node);
$newnode->setAttribute("name",$row['name']);
$newnode->setAttribute("address", $row['address']);
$newnode->setAttribute("lat", $row['lat']);
$newnode->setAttribute("lng", $row['lng']);
$newnode->setAttribute("type", $row['type']);
}
echo $dom->saveXML();
?>
另外,WHERE 1
SELECT 查询中描述了什么?我假设这是指 id 然后它会从那里继续 while 循环?