[英]Turning SQL into XML with variable Query
我想從數據庫中獲取值,並將其放入XML臨時文件中(對於Google Maps webapp),然后將其分配給輸入值。
我知道如果查詢是靜態的,該怎么做,例如Select * from markers where 1
但是我想做類似Select * from markers WHERE ID = $id
,而該$ id來自POST。
我有一個帶有JS的HTML文件,該文件調用PHP存檔以獲取XML並對其進行解析。 它適用於靜態查詢,但是如何使這些查詢動態化? 如何將該變量從JS函數傳遞到我的PHP文件?
這是獲取XML的代碼:
<?php
$username="SOMEUSER";
$password="SOMEPASSS";
$database="SOMEUSER_marcadoresMapas";
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=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 = "SELECT * FROM markers WHERE clave";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
header("Content-type: text/xml");
// Start XML file, echo parent node
echo '<markers>';
// Iterate through the rows, printing XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
echo '<marker ';
echo 'name="' . parseToXML($row['name']) . '" ';
echo 'address="' . parseToXML($row['address']) . '" ';
echo 'lat="' . $row['lat'] . '" ';
echo 'lng="' . $row['lng'] . '" ';
echo 'telefono="' . $row['telefono'] . '" ';
echo 'zona="' . $row['zona'] . '" ';
echo 'ciudad="' . $row['ciudad'] . '" ';
echo 'email="' . $row['email'] . '" ';
echo 'piso="' . $row['piso'] . '" ';
echo 'tipo="' . $row['tipo'] . '" ';
echo 'erasmus="' . $row['erasmus'] . '" ';
echo 'nhabitaciones="' . $row['nhabitaciones'] . '" ';
echo 'plazas="' . $row['plazas'] . '" ';
echo 'equipHabita="' . $row['equipHabita'] . '" ';
echo 'nbanos="' . $row['nbanos'] . '" ';
echo 'salon="' . $row['salon'] . '" ';
echo 'cocina="' . $row['cocina'] . '" ';
echo 'electrodomesticos="' . $row['electrodomesticos'] . '" ';
echo 'garaje="' . $row['garaje'] . '" ';
echo 'internet="' . $row['internet'] . '" ';
echo 'calefaccion="' . $row['calefaccion'] . '" ';
echo 'sexo="' . $row['sexo'] . '" ';
echo 'precio="' . $row['precio'] . '" ';
echo 'superficie="' . $row['superficie'] . '" ';
echo 'fecha="' . $row['fecha'] . '" ';
echo 'otros="' . $row['otros'] . '" ';
echo 'id="' . $row['id'] . '" ';
echo '/>';
}
//echo 'name="' . parseToXML('&','&', $row['name']) . '" ';
// End XML file
echo '</markers>';
?>
這是JS,它調用PHP文件並獲取XML:
function downloadUrl(url, callback) {
var request = window.ActiveXObject ?
new ActiveXObject('Microsoft.XMLHTTP') :
new XMLHttpRequest; //en cierto modo es una API, acepta requests HTTP.
request.onreadystatechange = function() {
if (request.readyState == 4) {
request.onreadystatechange = doNothing;
callback(request.responseText, request.status);
}
};
request.open('GET', url, true);
request.send(null);
}
downloadUrl("phpsqlajax_genxml2.php", function(data) {
var xml = parseXml(data);
var markers = xml.documentElement.getElementsByTagName("marker"); //coge todos los markers
for (var i = 0; i < markers.length; i++) { //coge los atributos de los markers
var name = markers[i].getAttribute("name");
var address = markers[i].getAttribute("address");
var telefono= markers[i].getAttribute("telefono");
var precio=markers[i].getAttribute("precio");
//var type = markers[i].getAttribute("type");
var point = new google.maps.LatLng( //crea LatLng a partir de Lat Long de los markers
parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
var html = "Nombre:" + name + "<br>Direccion:" + address+"<br>Telefono:"+telefono+"<br>Precio:"+precio;
var marker = new google.maps.Marker({ //posiciona los markers
map: map,
position: point
});
bindInfoWindow(marker, map, infoWindow, html); //pone la ventana de información
}
});
}
我想做的是類似函數downloadUrl(url,callback,id)並通過POST發送ID。
這可能還是我應該使用哪種方法?
非常感謝您,我沒有很好的解釋。
在JavaScript方面,您可以為downloadUrl()函數添加第三個參數。 並將open命令設置為使用POST而不是GET
request.open('POST', url, true);
並發送帶有請求的ID
var params = 'id='+id;
request.send(params);
在PHP方面,捕獲並過濾傳入的請求變量。 並將其添加到查詢中。
$id = filter_input(INPUT_POST, 'id', FILTER_SANITIZE_NUMBER_INT);
$query = "SELECT * FROM markers WHERE id = {$id}";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.