微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我有两个文件,file.PHP amd get_xml.PHP.我可以,在两个PHP文件中没有问题echo表信息,但是当我想使用搜索表单查询数据时,将其发送到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.

谢谢你的时间!

解决方法:

你可能会为此而踢自己.

$result = MysqL_query($result);

应该

$result = MysqL_query($query);

你不讨厌那个吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐