-1

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

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

任何人都可以告诉我为什么准备好的语句会出现这个错误?谢谢!问候!

4

1 回答 1

0

The mysql_prepare() line is failing and returning false, rather than a resource.

What columns are in the trans table? If you don't have exactly three columns, then that may cause your prepare error. You should always define your columns along with your values in an insert statement:

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

As long as any other columns existing in the database have either allowed NULL or a default value, then you should be able to insert the data.

EDIT:

I am unsure of whether you can bind the variables before they are set. I would recommend not binding them until after they contain values.

于 2013-08-01T20:26:06.900 回答