|
 |
|
ช่วยดูโค้ดการทำไฟล์ xml ให้ทีค่ะ ทำไมค่ามันไม่ยอมออก |
|
 |
|
|
 |
 |
|
Code (PHP)
<?php
header("Content-type: text/xml");
require("db.php");
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;
}
// Get parameters from URL
$center_lat = $_GET["latitude"];
$center_lng = $_GET["longitude"];
$radius = $_GET["radius"];
// Opens a connection to a MySQL server
$connection=mysql_connect (localhost, $username, $password);
if (!$connection) {
die('Not connected : ' . mysql_error());
}
// Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
// Select all the rows in the markers table
$query = sprintf("SELECT latitude, longitude, ( 3959 * acos( cos( radians('%s') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( latitude ) ) ) ) AS distance FROM place HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20",
mysql_real_escape_string($center_lat),
mysql_real_escape_string($center_lng),
mysql_real_escape_string($center_lat),
mysql_real_escape_string($radius));
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
// Start XML file, echo parent node
echo "<markers>\n";
// Iterate through the rows, printing XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
echo '<marker ';
echo 'name_place="' . parseToXML($row['name_place']) . '" ';
echo 'latitude="' . $row['latitude'] . '" ';
echo 'longitude="' . $row['longitude'] . '" ';
echo 'distance="' . $row['distance'] . '" ';
echo "/>\n";
}
// End XML file
echo "</markers>\n";
?>

Tag : PHP

|
|
 |
 |
 |
 |
Date :
2013-02-04 20:32:41 |
By :
sweetwater |
View :
737 |
Reply :
1 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ดูเหมือนว่า query จะให้ผลกลับมา 0 แถวนะครับ
เพราะถ้ามีแถวที่อ่านได้จากฐานข้อมูล ยังไงๆ ก็ต้องเข้าไปทำงานในลูป while ($row = @mysql_fetch_assoc($result)) แน่นอน
แต่นี่ไม่มี <marker /> โผล่ออกมา แสดงว่าไม่ได้เข้าไปทำงานในนั้นเลยครับ เพราะ $row ให้ค่าที่ทำให้เงื่อนไขเป็นเท็จ
ลองเช็คดูว่าได้ผลกลับมาบ้างหรือเปล่าด้วย mysql_num_rows() ดูครับ
Code (PHP)
...
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
if (!mysql_num_rows($result)) {
die('no rows found');
}
...
|
 |
 |
 |
 |
Date :
2013-02-05 05:56:59 |
By :
cookiephp |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 04
|