我有两个文件,file.php
和 get_xml.php
。我可以在两个 php 文件中毫无问题地回显表信息,但是当我想使用搜索表单查询数据时,将其发送到 get_xml.php
,我没有得到任何结果。
现在这是代码本身:
文件.php
<?php
$username="****";
$password="*******";
$database="******";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM markers";
$result=mysql_query($query);
$num=mysql_num_rows($result);
mysql_close();
?>
<form action="get_xml2.php" method="post">
Name: <input type="text" name="name"><br>
Address: <input type="text" name="address"><br>
Type: <input type="text" name="type"><br>
<input type="Submit">
</form>
get_xml2.php
<?php
require("db_access.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;
}
$name=$_POST['name'];
$address=$_POST['address'];
$type=$_POST['type'];
// 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 name, address, type FROM markers WHERE name = '%s' AND address = '%s' AND type = '%s'",
mysql_real_escape_string($name),
mysql_real_escape_string($address),
mysql_real_escape_string($type)
);
$result = mysql_query($result);
if($result == false) {
die(mysql_error() . "<br />\n$query");
}
if(mysql_num_rows($result) == 0) {
user_error("No rows returned by:<br />\n$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 = @mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
echo '<marker ';
echo 'name="' . parseToXML($row['name']) . '" ';
echo 'address="' . parseToXML($row['address']) . '" ';
echo 'type="' . parseToXML($row['type']) . '" ';
echo 'lat="' . $row['lat'] . '" ';
echo 'lng="' . $row['lng'] . '" ';
echo '/>';
}
// End XML file
echo '</markers>';
?>
当您进行搜索时,您会看到:
> Query was empty
SELECT name, address, type
FROM markers
WHERE name = 'The Melting Pot'
AND address = '14 Mercer St, Seattle, WA'
AND type = 'restaurant'
但是,当我将xml中的代码更改为
$query = "SELECT * FROM markers WHERE 1";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
The xml happily retrieves ALL data in from the database, as seen here .
感谢您的宝贵时间!
最佳答案
你可能会为此踢自己一脚。
$result = mysql_query($result);
应该是
$result = mysql_query($query);
你不讨厌那个吗? :-)
关于php - 提交搜索表单后未收到来自数据库的查询结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6669205/