dounao4179 2014-09-15 13:24
浏览 26
已采纳

这个PHP函数有什么问题?

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();

?>
  • 写回答

1条回答 默认 最新

  • douyin6188 2014-09-15 13:43
    关注
    //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...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后的密码
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面