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

标签 php mysql

我有两个文件,file.phpget_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.

现在这是代码本身:

文件.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 .

感谢您的宝贵时间!

最佳答案

你可能会为此踢自己一脚。

$result = mysql_query($result);

应该是

$result = mysql_query($query);

你不讨厌那个吗? :-)

关于php - 提交搜索表单后未收到来自数据库的查询结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6669205/

相关文章:

php - 如何使用 Drupal 发布驻留在外部数据库中的数据?

javascript - 在 Highcharts 中使用插件 CSV 导出时如何自定义文件名?

php - 从数据库中检索数据并在得到分隔符时分隔数据?

mysql - 无法使用 View 模块创建 View

javascript - 使用 ajax 和 php 的空响应

PHP 邮件 MYSQL 数据库

php - SNMP Php 无响应,在 linux shell 中工作

php - 如何使用 PHP 向用户发送每日电子邮件通知?

mysql - 在存储过程中打印多行

mysql - SQL Sum 每个团队的前 5 行