提交搜索表单后未收到数据库的查询结果

时间:2011-07-12 18:31:48

标签: php mysql

我有两个文件,file.php amd get_xml.php。我可以,在两个php文件中都没有问题回显表信息,但是当我想使用搜索表单查询数据时,将其发送到get_xml.php,我得不到任何结果。

Here is a working example along with all rows as a reference as to what is in the actual MySQL table.

现在这里是代码本身:

file.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('<','&lt;',$htmlStr); 
$xmlStr=str_replace('>','&gt;',$xmlStr); 
$xmlStr=str_replace('"','&quot;',$xmlStr); 
$xmlStr=str_replace("'",'&#39;',$xmlStr); 
$xmlStr=str_replace("&",'&amp;',$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

谢谢你的时间!

2 个答案:

答案 0 :(得分:5)

你可能会因此而踢自己。

$result = mysql_query($result);

应该是

$result = mysql_query($query);

难道你不讨厌吗? : - )

答案 1 :(得分:3)

你有一个错误:

$result = mysql_query($result);

而不是

$result = mysql_query($query);