开发者

Multiple prepared statements with MYSQLI

I just moved to mysqli and I was wondering: can I do a multiple query with the prepared statements?

Here is the example: I need to check if this username is also in the table "future_user" and not just in "user" as it is doing right now. For code appeal I'd rather not write again the same function just changing "user" with "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;

}

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 N开发者_JS百科ULL,
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;


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

With out seeing more of your table structure this what i can come up with. This will select the user in future user too


You could do a CROSS JOIN to connect the two tables and query them that way

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

You'll probably need to tweak that * so that identically named columns in the two tables don't overlay each other.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜