1
    <?php
  header("Content-type: text/xml");
  $names = array (
   "John Smith", "John Jones", "Jane Smith", "Jane Tillman",
   "Abraham Lincoln", "Sally Johnson", "Kilgore Trout",
   "Bob Atkinson","Joe Cool", "Dorothy Barnes",
   "Elizabeth Carlson", "Frank Dixon", "Gertrude East",
   "Harvey Frank", "Inigo Montoya", "Jeff Austin",
   "Lynn Arlington", "Michael Washington", "Nancy West" );
if (!$query) {
   $query=$_GET['query'];
}
echo "<?xml version=\"1.0\" ?>\n";
echo "<names>\n";
while (list($k,$v)=each($names)) {
   if (stristr($v,$query)) {
      echo "<name>$v</name>\n";
   }
}
echo "</names>\n";
?>

如您所见,此 PHP 只是在数组中查找名称。
我将此 php 文件放在我的 xampp 根目录中,然后在浏览器中键入 search.php?query=John,然后它告诉我
此页面包含以下错误:第 2 行第 1 列的错误:文档末尾的额外内容下面是第一个错误之前的页面渲染。
怎么了?

4

4 回答 4

1

试试这个:

 <?php
  header("Content-type: text/xml");
  $names = array (
   "John Smith", "John Jones", "Jane Smith", "Jane Tillman",
   "Abraham Lincoln", "Sally Johnson", "Kilgore Trout",
   "Bob Atkinson","Joe Cool", "Dorothy Barnes",
   "Elizabeth Carlson", "Frank Dixon", "Gertrude East",
   "Harvey Frank", "Inigo Montoya", "Jeff Austin",
   "Lynn Arlington", "Michael Washington", "Nancy West" );
var $query = NULL;

if (isset($_GET['query'])) {
   $query=$_GET['query'];
}
echo "<?xml version=\"1.0\" ?>\n";
echo "<names>\n";
while (list($k,$v)=each($names)) {
   if (stristr($v,$query)) {
      echo "<name>$v</name>\n";
   }
}
echo "</names>\n";
?>
于 2012-08-15T04:26:54.240 回答
1

我认为问题在于 -

if (!$query) {
   $query=$_GET['query'];
}

当我运行您的代码时,上面的代码出现错误。代码块不会做任何事情,因为 $query 似乎没有设置。

试试这个并确保设置了 $_GET["query"] 变量:

  $query=NULL;
    if (isset($_GET["query"])) {
       $query=$_GET['query'];
    }
于 2012-08-15T04:27:08.570 回答
0

您必须 $query = null;在使用它之前初始化变量。

于 2012-08-15T04:26:10.693 回答
0

改变

if (!$query) {
   $query= $_GET['query'];
}

if (!isset($query)) {
   $query= $_GET['query'];
}
于 2012-08-15T04:39:05.223 回答