我剛剛轉到mysqli,我想知道:我可以使用預處理語句執行多個查詢嗎?使用MYSQLI準備多個預處理語句
下面是示例:我需要檢查此用戶名是否也在表「future_user」中,而不僅僅是在「用戶」中,因爲它現在正在執行。對於代碼上訴,我寧願不再寫相同的功能,只是將「user」改爲「future_user」。
function isFreeUsername($string)
{
$DB = databaseConnect();
$stmt = $DB->prepare("SELECT * FROM user WHERE username=? LIMIT 1");
$stmt->bind_param("s", $username);
if(isset($_SESSION) && isset($_GET['username'])) $username = $_GET['username'];
else $username = $string;
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows > 0) $return = 0;
else $return = 1;
$stmt->close();
$DB->close();
return $return;
}
表:
CREATE TABLE user
(
uid mediumint(6) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
username varchar(15) NOT NULL,
password varchar(15) BINARY NOT NULL,
mail varchar(50) NOT NULL,
name varchar(50) NOT NULL,
surname varchar(50) NOT NULL,
birth char(10) NOT NULL,
sex tinyint(1) unsigned NOT NULL default 1,
address varchar(50) NOT NULL,
city varchar(50) NOT NULL,
zip char(5) NOT NULL,
province varchar(50) NOT NULL,
country tinyint(3) NOT NULL,
number1 varchar(50) NOT NULL,
number2 varchar(50) NOT NULL,
last_login TIMESTAMP,
registered TIMESTAMP,
online tinyint(1) unsigned default 0,
admin tinyint(1) unsigned default 0,
comment_allowed tinyint(1) unsigned default 0,
post_allowed tinyint(1) unsigned default 0
) ENGINE=InnoDB;
CREATE TABLE future_user
(
username varchar(15) NOT NULL,
password varchar(15) BINARY NOT NULL,
mail varchar(50) NOT NULL,
name varchar(50) NOT NULL,
surname varchar(50) NOT NULL,
birth char(8) NOT NULL,
sex tinyint(1) unsigned NOT NULL,
address varchar(50) NOT NULL,
city varchar(50) NOT NULL,
zip char(10) NOT NULL,
province varchar(50) NOT NULL,
country varchar(50) NOT NULL,
number1 varchar(50) NOT NULL,
number2 varchar(50) NOT NULL,
code char(10) NOT NULL
) ENGINE=InnoDB;
我不確定這個連接:user和future_user都不是uid字段。這兩個表中使用的用戶名無關:一個持有註冊用戶,另一個持有該用戶尚未確認。 – 2011-05-30 17:34:14