Thursday, March 16, 2017

Autocomplete with PHP, Jquery UI and Oracle - 2

searchUser.php

<?php
session_start();
date_default_timezone_set('EST');

    if ($_SERVER['REQUEST_METHOD'] === 'POST')
    {
        foreach ($_POST as $key => $value)
                eval("\$$key = \"$value\";");
    }

$userName = trim($_GET['term']);
$userName = strtoupper($userName);

putenv("TNS_ADMIN=/opt/.wallet");
putenv("ORACLE_HOME=/app/oracle/product/11.2.0.3");
putenv("LD_LIBRARY_PATH=/app/oracle/product/11.2.0.3/lib");

$conn = oci_connect("SYS", "password", "mydatabase", null, OCI_SYSDBA);
//$conn = oci_connect("/", "", "ITSERVICE", "AL32UTF8", OCI_CRED_EXT);

if (!$conn) {
        $m = oci_error();

        echo $m['message'] . "\n";
        exit;
} else {
        //$sql = 'BEGIN system.exposed_to_low_privs_users.GET_ACCOUNT_STATUSES(:P_OUT); END;';
        //$sql = "select username,ACCOUNT_STATUS from dba_users where username like '" . $userName . "%'";
        $sql = "select username,ACCOUNT_STATUS from dba_users order by USERNAME";
        $stmt = oci_parse($conn,$sql);

        // Bind the output parameter

        oci_execute($stmt);

                //oci_execute($cursor);

                $return_array=array();

                while (($row = oci_fetch_array($stmt, OCI_BOTH)) != false) {
                        $row_array['id'] = $row['USERNAME'];
                        $row_array['value'] = $row['USERNAME'];
                        $row_array['label'] =$row['USERNAME'];

                        //array_push($return_array,$row['USERNAME'],$row['ACCOUNT_STATUS']);
                        array_push($return_array,$row_array);
                }
                //var_dump($return_array);
                echo json_encode($return_array);
      }

 // Close the Oracle connection
 oci_free_statement($stmt);
 oci_close($conn);
 ?>

No comments:

Post a Comment