mysqli语句execute(),正确的方法

时间:2013-10-31 12:54:42

标签: php mysql mysqli

我在MyFile.php中有这段代码

$db= mysqli_connect("host","user","pw","db");//connect to db
if (mysqli_connect_errno($con))//check connection
{echo "Failed to connect to MySQL: " . mysqli_connect_error();}
//Create a token for the unique link
$title= $_GET[apt_title];
$email= $_GET[mail_address];
$token = sha1(uniqid($email, true));
$time = $_SERVER["REQUEST_TIME"];
//prepare the query to be executed
$query = $db->prepare(
"INSERT INTO pending_users (email, token, title_apt, tstamp) VALUES (?, ?, ?, ?)"
);
$query->execute(
array(
    $title,
    $email,
    $token,
    $time
)
);

MySQL的错误消息

Warning: mysqli_stmt::execute() expects exactly 0 parameters, 1 given in /websites

我花了几个小时试图解决这个问题,但我做不到。

有任何帮助吗?

3 个答案:

答案 0 :(得分:6)

因为mysqli::execute(),您会看到它不接受任何参数。在此之前,您必须准备查询,然后将参数绑定到查询。然后你必须调用execute()方法。所以试试这样:

$query = $db->prepare(
"INSERT INTO pending_users (email, token, title_apt) VALUES (?, ?, ?, ?)"
);
$db->bind_param($title, $email, $token, $time);
$db->execute();
$db->close();

有关详情,请查看documentation

答案 1 :(得分:1)

如果您查看mysqli::execute()的手册,您会发现它不接受任何参数。

  

bool mysqli_stmt :: execute(void)

相反,您应该使用mysqli::bind_param来绑定参数。

答案 2 :(得分:1)

你需要在执行查询之前绑定params, 以程序的方式做这样的

mysqli_stmt_execute($stmt);

如果你在绑定params之后就像面向对象那样做了

/* Execute the statement */
$stmt->execute();

文档链接。

http://www.php.net/manual/en/mysqli-stmt.execute.php