2

最近、以前は正常に機能していたlibのエラーに遭遇しました。それがどこにあるかを理解できれば、気が滅入るでしょう。

コードサンプルを以下に示します。その中にあるデバッグ機能についてお詫びしますが、動作させようとしています。

問題は、$ tempが正しいキー(列の名前)を持つ配列であるが、すべての値がNULLであるということです。

問題は

call_user_func_array(array($query, 'bind_result'), $params);

少しですが、頭を包むことはできません。

public function fetchRows(){
    error_reporting(E_ALL+E_NOTICE);
    $args = func_get_args();
    $sql = array_shift($args);
    traceVar($sql, "Query");
    $colTypes = array_shift($args);
    if (!$query = $this->prepare($sql, $colTypes)) {
        die('Please check your sql statement : unable to prepare');
    }
    if (count($args)){
        traceVar($args,'Binding params with');
        call_user_func_array(array($query,'bindParam'), $args);
    }

    $query->execute();

    $meta = $query->result_metadata();
    while ($field = $meta->fetch_field()) {
        $params[] = &$row[$field->name];
    }
    traceVar($params,'Binding results with');
    call_user_func_array(array($query, 'bind_result'), $params);

    while ($query->fetch()) {
        traceVar($row,'After fetch');
        $temp = array();
        foreach($row as $key => $val) {
            $temp[$key] = $val;
        } 
        $result[] = $temp;
    }

    $meta->free();
    $query->close(); 
    //self::close_db_conn(); 
    return $result;
}
4

2 に答える 2

3

起動時にサーバーを選択できれば、PHP用のphp-mysqlモジュールの代わりにphp-mysqlndモジュールを使用できます。(または、すでに使用している場合は、「phpinfo();」を実行して「mysqlnd」を検索してください):

public function fetchRows(){
    ...
    $query->execute();

    $res = $query->get_result();
    while (($row = $res->fetch_assoc()))
        $result[] = $row;
    return $result;
    }
}

それは私には簡単に思えます。

于 2014-04-04T10:40:08.823 に答える
2

あなたが提供したコードは私のために働きます。

この関数は、配列の各要素をメソッド引数として指定したかのように、指定された配列を使用してオブジェクトのまたはメソッドをcall_user_func_array(...)呼び出すだけです。bindParambind_result$query

以下のコードを使用して、問題が発生しているSQLステートメントを確認することをお勧めします。元のコードは抽象化レイヤーのステートメントクラスに依存しているため、完全にテスト可能にするために少し書き直しました。

<?php

$db_host = 'localhost';
$db_user = 'username';
$db_pass = 'password';
$db_name = 'database';

$mysqli = new mysqli($db_host, $db_user, $db_pass, $db_name);

print_r(fetchRows('SELECT something from some_table WHERE some_id = ?', 'i', 1));

function traceVar($a, $b) {
    print_r(array($b => $a));
}

function fetchRows(){
        error_reporting(E_ALL+E_NOTICE);
        $args = func_get_args();
        $sql = array_shift($args);
        traceVar($sql, "Query");

        // Keep the column types for bind_param.
        // $colTypes = array_shift($args);

        // Column types were originally passed here as a second
        // argument, and stored in the statement object, I suppose.
        if (!$query = $GLOBALS['mysqli']->prepare($sql)){ //, $colTypes)) {
                die('Please check your sql statement : unable to prepare');
        }
        if (count($args)){
                traceVar($args,'Binding params with');

                // Just a quick hack to pass references in order to
                // avoid errors.
                foreach ($args as &$v) {
                    $v = &$v;
                }

                // Replace the bindParam function of the original
                // abstraction layer.
                call_user_func_array(array($query,'bind_param'), $args); //'bindParam'), $args);
        }

        $query->execute();

        $meta = $query->result_metadata();
        while ($field = $meta->fetch_field()) {
                $params[] = &$row[$field->name];
        }
        traceVar($params,'Binding results with');
        call_user_func_array(array($query, 'bind_result'), $params);

        while ($query->fetch()) {
                traceVar($row,'After fetch');
                $temp = array();
                foreach($row as $key => $val) {
                        $temp[$key] = $val;
                } 
                $result[] = $temp;
        }

        $meta->free();
        $query->close(); 
        //self::close_db_conn(); 
        return $result;
}
于 2009-12-09T09:36:24.743 に答える