【问题标题】:Autocomplete PHP JQuery Oracle自动完成 PHP JQuery Oracle
【发布时间】:2017-12-11 20:14:05
【问题描述】:

我正在尝试让 Autocomplete 与 Oracle 数据库一起使用。

我找到了测试代码并试图让它与 Oracle 一起工作,但它不起作用,我不知道问题是在未触及的搜索表单中还是在 backend-search.php(原始代码还在代码中用 cmets #)

如果我在文本框中输入任何内容。

数据库连接在另一个表单上测试,我将数据放入表中。

如果有人可以给我一个提示,我将不胜感激。

搜索表单.php

<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<title>PHP Live MySQL Database Search</title>
<style type="text/css">
    body{
        font-family: Arail, sans-serif;
    }
    /* Formatting search box */
    .search-box{
        width: 300px;
        position: relative;
        display: inline-block;
        font-size: 14px;
    }
    .search-box input[type="text"]{
        height: 32px;
        padding: 5px 10px;
        border: 1px solid #CCCCCC;
        font-size: 14px;
    }
    .result{
        position: absolute;        
        z-index: 999;
        top: 100%;
        left: 0;
    }
    .search-box input[type="text"], .result{
        width: 100%;
        box-sizing: border-box;
    }
    /* Formatting result items */
    .result p{
        margin: 0;
        padding: 7px 10px;
        border: 1px solid #CCCCCC;
        border-top: none;
        cursor: pointer;
    }
    .result p:hover{
        background: #f2f2f2;
    }
</style>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $('.search-box input[type="text"]').on("keyup input", function(){
        /* Get input value on change */
        var inputVal = $(this).val();
        var resultDropdown = $(this).siblings(".result");
        if(inputVal.length){
            $.get("backend-search.php", {term: inputVal}).done(function(data){
                // Display the returned data in browser
                resultDropdown.html(data);
            });
        } else{
            resultDropdown.empty();
        }
    });       
    // Set search input value on click of result item
    $(document).on("click", ".result p", function(){
        $(this).parents(".search-box").find('input[type="text"]').val($(this).text());
        $(this).parent(".result").empty();
    });
});
</script>
</head>
<body>
    <div class="search-box">
        <input type="text" autocomplete="off" placeholder="Stationsname..." />
        <div class="result"></div>
    </div>
</body>
</html>

后端搜索.php

<?php
error_reporting(E_ALL);

#$link = mysqli_connect("localhost", "root", "", "demo");
    $db = '(DESCRIPTION =(ADDRESS =(PROTOCOL = TCP)(Host = xxxxxx)(Port = 1521))(CONNECT_DATA = (SERVICE_NAME = xxxxx) ))';
    $dbuser = 'xxxxx';
    $pass = 'xxxxx';
    $conn = oci_connect($dbuser, $pass, $db);

if(isset($_REQUEST['term'])){
    // Prepare a select statement
    #$sql = 'SELECT Standortname FROM DATABASE WHERE Standortname LIKE :s';
    #if($stmt = mysqli_prepare($conn, $sql)){
    if($stmt = oci_parse($conn, "SELECT Standortname FROM Database WHERE Standortname LIKE :s")){
        // Bind variables to the prepared statement as parameters
        #mysqli_stmt_bind_param($stmt, "s", $param_term);
        oci_bind_by_name( $stmt , ':s' , $param_term, -1 ); #$ph_name
        // Set parameters
        $param_term = $_REQUEST['term'] . '%';
        // Attempt to execute the prepared statement
        #if(mysqli_stmt_execute($stmt)){
        if(oci_execute($stmt)){
            #$result = mysqli_stmt_get_result($stmt);
            $result = oci_result($stmt);
            // Check number of rows in the result set
            #if(mysqli_num_rows($result) > 0){
            if(oci_num_rows($result) > 0){
                // Fetch result rows as an associative array
                #while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
                while($row = oci_fetch_array($result, OCI_BOTH)){
                    echo "<p>" . $row["name"] . "</p>";
                }
            } else{
                echo "<p>No matches found</p>";
            }
        } else{
            echo "ERROR: Could not able to execute"; #$sql. " . mysqli_error($conn);
        }
    }

    // Close statement
    #mysqli_stmt_close($stmt); 
    oci_free_statement($stmt);
// close connection
#mysqli_close($conn);
oci_close($conn);
?>

【问题讨论】:

    标签: php jquery oracle autocomplete


    【解决方案1】:

    请,请,在开发过程中查看error log file。您有一个语法错误,导致脚本无法运行。在这种情况下,仅仅拥有error_reporting(E_ALL); 并不能帮助您:

    解析错误:语法错误,...中的文件意外结束

    这是因为if(isset($_REQUEST['term'])) { 没有关闭}。通过在文件末尾添加} 来修复它。

    现在您会看到两个警告:

    1. 警告:oci_result() 需要 2 个参数,其中 1 个在 ...

      由于没有在$result = oci_result($stmt); 中提供所需的第二个参数。

    2. 警告:oci_num_rows() 期望参数 1 是资源,在 ... 中给出布尔值

      由错误处理if(oci_num_rows($result) &gt; 0){中的返回值引起

    看起来您只是天真地用 oci 函数替换了 mysql 函数,而不考虑行为差异。

    ...
    if (oci_execute($stmt)) {
        while (($row = oci_fetch_array($stmt, OCI_BOTH)) != false) {
            // MUST use uppercase column names.
            echo "<p>" . $row["STANDORTNAME"] . "</p>";
        }
    }
    ...
    

    【讨论】:

    • 您好,蒂姆,感谢您的意见。我真的不熟悉这些东西,但在你的帮助下,我能够让它工作。非常感谢你
    【解决方案2】:
    if(isset($_REQUEST['term']) and (strlen($_REQUEST['term']) >= 3)){
        if($stmt = oci_parse($conn, "SELECT LOCATIONNAME from DATABASE where LOCATIONNAME like  :s  and AKTIV = 'Y'")){
        $param_term = '%' . $_REQUEST['term'] . '%';    
        oci_bind_by_name( $stmt , ":s" , $param_term, -1);                                                                  
            if(oci_execute($stmt)){                                                                                         
                    while(($row = oci_fetch_array($stmt, OCI_BOTH)) != false) {                                                         
                        echo "<p>" . $row['LOCATIONNAME'] . "</p>"; # "
                    }
            } else{
                echo "ERROR: Could not able to execute" . $param_term; 
            }
        }
        oci_free_statement($stmt);   
    

    ......

    这对我有用。

    BR

    【讨论】:

      猜你喜欢
      • 2016-04-16
      • 2011-07-15
      • 2015-09-25
      • 2017-11-11
      • 2012-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多