使用MYSQLI的多个预准备语句

时间:2011-05-29 23:24:10

标签: php mysql

我刚搬到mysqli,我想知道:我可以用准备好的语句进行多次查询吗?

以下是示例:我需要检查此用户名是否也在表“future_user”中,而不是仅仅在“user”中,因为它现在正在执行。对于代码吸引力,我宁愿不再使用“future_user”更改“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;

}

TABLES:

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;

2 个答案:

答案 0 :(得分:1)

"SELECT * 
 FROM user u
 LEFT JOIN future_user fu on fu.id = u.id 
 WHERE u.username=?
 LIMIT 1"

没有看到更多的桌面结构,这就是我能想到的。 这也将在未来的用户中选择用户

答案 1 :(得分:0)

您可以执行CROSS JOIN连接两个表并以这种方式查询

SELECT * FROM user JOIN future_user 
    WHERE user.username = ? OR future_user.username = ?

您可能需要调整*,以便两个表中具有相同名称的列不会相互叠加。