mysqli_prepare查询返回false

时间:2017-06-15 00:56:16

标签: php mysql database mysqli parameters

我试图使用call_user_func_array将params绑定到sql语句,如Dynamically Bind Params in Prepared Statements with MySQLi所述;但是,我的mysqli_prepare一直都是假的。

这是我的数据函数,用于存储数据:

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字符串:

$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)"

以下是我的$params$key输出:

$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"

1 个答案:

答案 0 :(得分:1)

我试图映射的列中存在错误,但不存在。找到错误过程here,这使我产生致命错误,指出我引用的表中不存在列。