2017-07-07 156 views
0

我試圖讓Autocomplete與Oracle數據庫一起工作。自動完成PHP JQuery Oracle

我發現了測試代碼,並試圖讓它與Oracle一起工作,但它不起作用,我不知道問題是在未觸及的搜索表單中還是在backend-search.php(原始代碼是仍然在代碼評論#)

如果我在文本框中鍵入沒有任何反應。

數據庫連接在另一個表單上進行測試,我將數據獲取到表中。

如果有人能給我一個提示,我將不勝感激。

搜索form.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> 

後端-search.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); 
?> 

回答

0
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

0

請,請,請error log file而發展。你有一個語法錯誤,阻止腳本運行。在這種情況下,僅僅有error_reporting(E_ALL);是不會幫助你:

Parse error: syntax error, unexpected end of file in ...

它通過不爲if(isset($_REQUEST['term'])) {關閉}引起的。通過在文件末尾添加一個}來修復它。現在

你會看到兩個警告:

  1. Warning: oci_result() expects exactly 2 parameters, 1 given in ...

    $result = oci_result($stmt);未提供所需的第二個參數引起的。

  2. Warning: oci_num_rows() expects parameter 1 to be resource, boolean given in ...

    由不正確地處理返回值if(oci_num_rows($result) > 0){

它看起來像你剛剛更換天真功能mysqloci功能,而在行爲差異的任何方面引起的。

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

Hy Tim,謝謝您的輸入。我真的不熟悉這個東西,但在你的幫助下,我能夠得到它的工作。非常感謝 – Fox83