I'm trying to get Autocomplete to work with an Oracle DB.
I found code for testing and tried to get it to work with Oracle but it doesn't work and I don't know if the problem is in the search-form which is untouched or in the backend-search.php
(original code is still in the code with comments #)
If I type in the textbox nothing happens.
The database connection is tested on another form where I get the data into a table.
If somebody can give me a hint I would be grateful.
search-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>
backend-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);
?>
Please, please, please look at the error log file whilst developing. You have a syntax error that prevents the script from running. In this case just having error_reporting(E_ALL);
isn't going to help you:
Parse error: syntax error, unexpected end of file in ...
It's caused by not having a closing }
for if(isset($_REQUEST['term'])) {
. Fix it by adding a }
at the end of the file.
Now you'll see two warnings:
Warning: oci_result() expects exactly 2 parameters, 1 given in ...
Caused by not providing the required second parameter in $result = oci_result($stmt);
.
Warning: oci_num_rows() expects parameter 1 to be resource, boolean given in ...
Caused by incorrectly handling the return value in if(oci_num_rows($result) > 0){
It looks like you've just naively replaced mysql
functions with oci
functions without any regard for differences in behaviour.
...
if (oci_execute($stmt)) {
while (($row = oci_fetch_array($stmt, OCI_BOTH)) != false) {
// MUST use uppercase column names.
echo "<p>" . $row["STANDORTNAME"] . "</p>";
}
}
...
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);
......
This worked for me.
BR
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.