简体   繁体   中英

What is wrong with this PHP function?

I wrote a PHP function to query a database and return the complete result set but it's not working (I get no results). I'm new to PHP, am I using the function properly?

<?php

$sql = array(
    'user'     => 'user',
    'password' => 'pass',
    'server'   => '10.10.10.10', 
    'db'       => 'XE'
);

$conn = oci_connect($sql['user'], $sql['password'], $sql['server'].'/'.$sql['db']);

if (!$conn) {
    $e = oci_error();
    trigger_error( htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR );
}

function db_query( $conn, $bindings, $query )
{
    $stmt = oci_parse( $conn, trim($query) );

    foreach ($bindings as $key => $value) {
        if ( strpos( $query, $key) ) {
            oci_bind_by_name( $stmt, $key, $value );
        }
    }

    oci_execute( $stmt );
    oci_fetch_all( $stmt, $data );
    oci_free_statement( $stmt );

    return $data;
}

$bindings = array();
$query    = 'SELECT COUNT(*) FROM Orders';

echo db_query();

?>
//Set your bindings
$bindings = array();
//Set your Query
$query    = 'SELECT COUNT(*) FROM Orders';
//Fire it up and store the result in $data
$data = db_query( $conn, $bindings, $query );
//Dump $data to see whats inside
var_dump($data);

This should help you... If $data returns NULL, make sure the query is correct, the databasetable is filled with data and the database connection is successful.

Steps:

  1. update your code

  2. dump $data and check the result

  3. check the databse connection

  4. check the databasetable

  5. have a drink...

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM