1

SQL ステートメントを生成するコードは問題なく動作しますが、$stmt->bind_param の文字列を生成するときに問題が発生します。コードは次のとおりです。

$stmt = $mysqli->stmt_init(); if ($stmt->prepare ($sql)) { $bind_types = '"'; $bind_values = '';

    if ($action == 'insert' || $action == 'update') {
        reset ($array);
        foreach ($array as $key => $value) {
            if (is_string ($value)) { $type = 's'; } else if (is_int ($value)) { $type = 'i'; } else if (is_float ($value)) { $type = 'd'; } else { die ('Cannot determine type for ' . $key . ' => ' . $value . ''); }
            $bind_types .= $type;
            $bind_values .= $value . ', ';
            //$stmt->bind_param ($type, $value);
        }
    }

    if ($action == 'update' || $action == 'delete') {
        if (is_string ($id_value)) { $type = 's'; } else if (is_int ($id_value)) { $type = 'i'; } else if (is_float ($id_value)) { $type = 'd'; } else { die ('Cannot determine type for ' . $id_column . ' => ' . $id_value . ''); }
        $bind_types .= $type;
        $bind_values .= $id_value . ', ';
        //$stmt->bind_param ($type, $id_value);
    }

    $bind_types .= '"';

    $bind_values = substr ($bind_values, 0, -2);

    echo $bind_types  . ', ' . $bind_values;

    $stmt->bind_param ($bind_types, $bind_values);
    $stmt->execute();

}

そのフォーマットが台無しになりました。読みづらかったらすいません。

次のエラーが表示されます。

「警告: mysqli_stmt::bind_param() [mysqli-stmt.bind-param]: 型定義文字列の要素数がバインド変数の数と一致しません ...」

何か案は?

4

2 に答える 2

3

I would highly advise to use PDO as you can do it easily. If you want to do it in mysqli it is more complicated since you can't easily bind them dynamically. To bind them dynamically look at this ugly hack

$bind_values= explode(',', $bind_values);
call_user_func_array(array($stmt, 'bind_param'), makeValuesReferenced($bind_values));
$stmt->execute();

function makeValuesReferenced(&$arr){ 
    $refs = array(); 
    foreach($arr as $key => $value) 
        $refs[$key] = &$arr[$key]; 
    return $refs; 

}
于 2012-07-18T02:47:08.857 に答える
0

への呼び出しbind_paramが間違っています: 次のようにする必要があります:

bind_param($types, $value1, $value2, $value3 ...);

これらの各値は実際の変数です。

于 2012-07-18T02:36:48.043 に答える