2

我在 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
)
);

错误信息:

警告:mysqli_stmt::execute() 需要 0 个参数,1 个在 /websites 中给出

我应该如何调用execute()正确的方法?

4

3 回答 3

7

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

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

有关更多信息,请查看文档

于 2013-10-31T12:59:33.113 回答
3

您需要在执行查询之前绑定参数,以程序方式这样做

mysqli_stmt_execute($stmt);

如果您在绑定参数后以面向对象的方式进行操作

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

文档链接。

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

于 2013-10-31T13:03:54.460 回答
1

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

bool mysqli_stmt::execute ( 无效 )

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

于 2013-10-31T13:00:22.773 回答