問(wèn)題描述
我有一個(gè)函數(shù)可以做到這一點(diǎn):
I have a function that does this:
function registerUser($firstName, $lastName, $address, $postcode, $email, $password)
{
$params = array($firstName, $lastName, $address, $postcode, $email, $password);
$result = $this->db->bind("INSERT INTO Users VALUES (?, ?, ?, ?, ?, ?)", 'ssssss', $params);
}
發(fā)送到我的數(shù)據(jù)庫(kù)類(lèi),它執(zhí)行以下操作:
Which sends off to my database class, which does this:
public function bind($query, $type, $params)
{
$this->query = $query;
$stmt = $this->mysqli->prepare($this->query);
$stmt->bind_param($type, $param);
$stmt->execute;
}
問(wèn)題是這行不通.
我希望做的是獲取 $params
列表并讓它在 $type
之后列出它們,這樣查詢將類(lèi)似于:>
What I was hoping to do, was to take the $params
list and have it list them after the $type
, so that the query would resemble:
$stmt->bind_param('ssssss', $firstName, $lastName, $address, $postcode, $email, $password);
但顯然我的做法是錯(cuò)誤的.
But obviously I'm going about it the wrong way.
有沒(méi)有辦法讓數(shù)組...轉(zhuǎn)換成一個(gè)列表,以便在 bind_param
查詢階段打印出來(lái)?
is there a way to make the array...transform as it were, into a list to be printed out at the bind_param
query stage?
推薦答案
call_user_func_array使用參數(shù)數(shù)組調(diào)用回調(diào)"
call_user_func_array "Call a callback with an array of parameters"
call_user_func_array(array($stmt, "bind_param"), array_merge(array($type), $params));
應(yīng)該做的工作
UPDATE:您還必須更改您的 params 數(shù)組:
UPDATE: you have also to change your params array:
$params = array(&$firstName, &$lastName, &$address, &$postcode, &$email, &$password);
as mysqli_stmt::bind_param
期待第二個(gè)和以下參數(shù)的引用.
as mysqli_stmt::bind_param
expects the second and the following parameters by reference.
您的查詢似乎是錯(cuò)誤的.也許你的字段比變量少.做:
Your query seems to be wrong. Maybe you have less fields than you have variables there. Do:
"INSERT INTO Users (field1, field2, field3, field4, field5, field6) VALUES (?, ?, ?, ?, ?, ?)"
用正確的名稱(chēng)替換字段的名稱(chēng)
where you replace the name of the fields by the correct names
這篇關(guān)于將參數(shù)與參數(shù)數(shù)組綁定的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!