【问题标题】:Live search with PHP AJAX and XML使用 PHP AJAX 和 XML 进行实时搜索
【发布时间】:2015-06-12 02:48:56
【问题描述】:

我在网络上进行了搜索,并在 w3schools 上找到了这个链接,该链接告诉您如何使用 Php、Ajax 和 XML (link) 进行实时搜索。我可以理解他们在下面的代码上做了什么......

search.php 文件

<?php
include_once 'header.php';
?>
<html>
<head>
<script>
function showResult(str) {
  if (str.length==0) {
    document.getElementById("livesearch").innerHTML="";
    document.getElementById("livesearch").style.border="0px";
    return;
  }
  if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  } else {  // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
      document.getElementById("livesearch").innerHTML=xmlhttp.responseText;
      document.getElementById("livesearch").style.border="1px solid #A5ACB2";
    }
  }
  xmlhttp.open("GET","livesearch.php?q="+str,true);
  xmlhttp.send();
}
</script>
</head>
<body>

<form>
<input type="text" size="30" onkeyup="showResult(this.value)">
<div id="livesearch"></div>
</form>

</body>
</html> 

还有 livesearch.php 文件

<?php
$xmlDoc=new DOMDocument();
$xmlDoc->load("links.xml");

$x=$xmlDoc->getElementsByTagName('link');

//get the q parameter from URL
$q=$_GET["q"];

//lookup all links from the xml file if length of q>0
if (strlen($q)>0) {
  $hint="";
  for($i=0; $i<($x->length); $i++) {
    $y=$x->item($i)->getElementsByTagName('title');
    $z=$x->item($i)->getElementsByTagName('url');
    if ($y->item(0)->nodeType==1) {
      //find a link matching the search text
      if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q)) {
        if ($hint=="") {
          $hint="<a href='" .
          $z->item(0)->childNodes->item(0)->nodeValue .
          "' target='_blank'>" .
          $y->item(0)->childNodes->item(0)->nodeValue . "</a>";
        } else {
          $hint=$hint . "<br /><a href='" .
          $z->item(0)->childNodes->item(0)->nodeValue .
          "' target='_blank'>" .
          $y->item(0)->childNodes->item(0)->nodeValue . "</a>";
        }
      }
    }
  }
}

// Set output to "no suggestion" if no hint was found
// or to the correct values
if ($hint=="") {
  $response="no suggestion";
} else {
  $response=$hint;
}

//output the response
echo $response;
?> 

但是他们接下来要做的是有一个 xml 页面 (link),其中包含他们想要搜索的所有数据,但在我的情况下,我想搜索我的数据库表并且我正在使用 SQL。我尝试进行一些编码,但我找不到如何从查询中获取数据。

links.xml 文件

<?php

error_reporting(E_ALL);

$host       = "localhost";
$user       = "root";
$pass       = "smogi";
$database   = "project";


$SQL_query = "SELECT * FROM patient WHERE fname = ???? OR lname = ????";

?>

<pages>
    <link>
        <title>Also display here the name of the user</title>
        <url>members2.php?view=?????</url>
    </link>
</pages>

凡我有 ???? 意味着我不知道该写什么。也许xml的代码需要更多的代码。

你能帮我修复我的 xml 并让它显示我的数据库中的结果吗

【问题讨论】:

    标签: php jquery mysql ajax xml


    【解决方案1】:

    您需要编辑的文件是 livesearch.php 文件。 Links.xml 被 livesearch.php 作为数据源读取,在您的情况下是数据库。修改后的 livesearch.php 如下所示:

    <?php
    $host       = "localhost";
    $user       = "root";
    $pass       = "Passw0rd";
    $database   = "project";
    
    $db = new PDO("mysql:host={$host};dbname={$database}", $user, $pass);
    $stmt = $db->prepare("SELECT * FROM patient WHERE fname LIKE :q OR lname LIKE :q");
    $stmt->bindValue(':q', '%'.$_GET['q'].'%');
    $stmt->execute();
    
    while ( $row = $stmt->fetchObject() ) {
        echo '<a href="members2.php?view=' . $row->username . '" target="_blank">' . $row->fname . ' ' . $row->lname . '</a><br/>';
    }
    ?>
    

    这将产生与 w3schools 提供的 livesearch.php 示例类似的输出。

    【讨论】:

    • 所以如果我理解你说我只需要 search.php 和 livesearch.php 文件。并使用您的代码更改 livesearch.php 文件中的所有代码。正确的?如果我按照你说的做,我会收到此错误Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [1044] Access denied for user ''@'localhost' to database 'project'' in E:\xampp\htdocs\ptixiaki\livesearch.php:7 Stack trace: #0 E:\xampp\htdocs\ptixiaki\livesearch.php(7): PDO-&gt;__construct('mysql:host=loca...') #1 {main} thrown in E:\xampp\htdocs\ptixiaki\livesearch.php on line 7
    • PDO 构造函数需要有效的dsn,username,password。所以在你的情况下,$db = new PDO("mysql:host={$host};dbname={$database}", $user, $pass);。阅读说明书会对你有很大帮助! php.net/pdo :)
    • @tftd 首先感谢您抽出宝贵时间。我按照你说的做了,现在我得到了这个Fatal error: Cannot pass parameter 2 by reference in E:\xampp\htdocs\ptixiaki\livesearch.php on line 9,第9行是这个$stmt-&gt;bindParam(':q', '%'.$_GET['q'].'%');
    • 您可能需要在查询中设置两个不同的参数,而不仅仅是一个。所以第一个:q 应该变成:q1 和第二个:q2。然后通过 $stmt-&gt;bindParam(':q1', '%'.$_GET['q'].'%')$stmt-&gt;bindParam(':q2', '%'.$_GET['q'].'%') 将值绑定到这些参数。
    • 是的,类似的东西应该可以工作。尝试用bindValue替换bindParam
    猜你喜欢
    • 2017-09-13
    • 1970-01-01
    • 2019-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-13
    • 2019-01-14
    • 1970-01-01
    相关资源
    最近更新 更多