我正试图使用call_user_func_array将参数绑定到sql语句,如Dynamically Bind Params in Prepared Statements with MySQLi中所述;但是,我的mysqli_prepare一直返回false。
以下是调用以存储数据的数据函数:

function storeData($form_data, $table_name, $cxn){
if(!is_array($form_data)){
    return false;
    exit();
}
$types = str_repeat("s", count($form_data));
$params = array();
$params[] = &$types;
$keys = array_keys($form_data);
$values = array_values($form_data);
for ($i = 0; $i < count($values); $i++) {
    $params[] = &$values[$i];
}

$sql = "INSERT INTO $table_name (" . implode(',', $keys) . ") VALUES (" .
    implode(',', array_fill(0, count($values), '?')) . ")
    ON DUPLICATE KEY UPDATE ";
$updates = implode(',', array_map(function($col) {
    return "$col = VALUES($col)";
}, $keys));
$sql .= $updates;

if($stmt = mysqli_prepare($cxn, $sql)){
    call_user_func_array(array($stmt, 'bind_param'), $params);
    return mysqli_stmt_execute($stmt);
}

这是我准备时的字符串:
$sql"INSERT INTO interest (Baseball,Basketball,Camping,Canoeing,Cycling,Football,Gaming,Golf,Hiking,Parks,Photography,Runway,Skydiving,Soccer,Username) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE Baseball = VALUES(Baseball),Basketball = VALUES(Basketball),Camping = VALUES(Camping),Canoeing = VALUES(Canoeing),Cycling = VALUES(Cycling),Football = VALUES(Football),Gaming = VALUES(Gaming),Golf = VALUES(Golf),Hiking = VALUES(Hiking),Parks = VALUES(Parks),Photography = VALUES(Photography),Runway = VALUES(Runway),Skydiving = VALUES(Skydiving),Soccer = VALUES(Soccer),Username = VALUES(Username)"

这是我的$sql$params输出:
$keysarray[15]
$keys[0]"Baseball"
$keys[1]"Basketball"
$keys[2]"Camping"
$keys[3]"Canoeing"
$keys[4]"Cycling"
$keys[5]"Football"
$keys[6]"Gaming"
$keys[7]"Golf"
$keys[8]"Hiking"
$keys[9]"Parks"
$keys[10]"Photography"
$keys[11]"Runway"
$keys[12]"Skydiving"
$keys[13]"Soccer"
$keys[14]"Username"

$paramsarray[16]
$params[0]"sssssssssssssss"
$params[1]"0"
$params[2]"0"
$params[3]"0"
$params[4]"0"
$params[5]"0"
$params[6]"0"
$params[7]"0"
$params[8]"0"
$params[9]"0"
$params[10]"0"
$params[11]"0"
$params[12]"0"
$params[13]"0"
$params[14]"0"
$params[15]"test0613"

$valuesarray[15]
$values[0]"0"
$values[1]"0"
$values[2]"0"
$values[3]"0"
$values[4]"0"
$values[5]"0"
$values[6]"0"
$values[7]"0"
$values[8]"0"
$values[9]"0"
$values[10]"0"
$values[11]"0"
$values[12]"0"
$values[13]"0"
$values[14]"test0613"

最佳答案

在试图映射不存在的列中存在错误。发现错误程序here,这允许我产生致命错误,指出在我引用的表中不存在列。

关于php - mysqli_prepare查询返回false,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44556899/

10-12 12:57