bind param =>错误,为什么?

时间:2013-08-01 20:05:46

标签: php mysqli statements

嘿那里我只想在我的数据库中插入一些值,我使用了这段代码:

define('SECURE', true);
include "storescripts/connect_to_mysql.php";

if (!$mysqli) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$stmt = mysqli_prepare($mysqli, "INSERT INTO `trans` VALUES (?, ?, ?)");
mysqli_stmt_bind_param($stmt, 'sssd', $txn_id, $payer_email, $mc_gross);

$txn_id = 123456789;
$payer_email = 'someone@example.com';
$mc_gross = 100;

/* execute prepared statement */
mysqli_stmt_execute($stmt);

printf("%d Row inserted.\n", mysqli_stmt_affected_rows($stmt));

/* close statement and connection */
mysqli_stmt_close($stmt);

调用此脚本后,我得到了这个:

Warning: mysqli_stmt_bind_param() expects parameter 1 to be mysqli_stmt, boolean given

Warning: mysqli_stmt_execute() expects parameter 1 to be mysqli_stmt, boolean given

Warning: mysqli_stmt_affected_rows() expects parameter 1 to be mysqli_stmt, boolean given 

0 Row inserted. 

Warning: mysqli_stmt_close() expects parameter 1 to be mysqli_stmt, boolean given

任何人都可以告诉我为什么预备语句会出现此错误?谢谢!问候!

1 个答案:

答案 0 :(得分:0)

mysql_prepare()行失败并返回false,而不是资源。

trans表中有哪些列?如果您没有正好三列,则可能会导致准备错误。您应该始终在insert语句中定义列和值:

INSERT INTO `trans` (`txn_id`, `payer_email`, `mc_gross`) VALUES (?, ?, ?)

只要数据库中存在的任何其他列都允许NULL或默认值,那么您应该能够插入数据。

修改

我不确定您是否可以在设置之前绑定变量。我建议在它们包含值之前不要绑定它们。