Php 在MSSQL中连接三个表
我在MSSQL中有三个游戏表,其结构如下: 查尔斯 这是它的查找表:Php 在MSSQL中连接三个表,php,sql-server,join,Php,Sql Server,Join,我在MSSQL中有三个游戏表,其结构如下: 查尔斯 这是它的查找表: strAccount链接到NGSCUSER的主键,它是 帐户用户名 strChar01是第一个字符槽 strChar02是第二个字符槽 strChar03是第三个字符槽 其中包含gameuser表的主键,它们是角色名 斯库塞 strUserID帐户用户名 strPasswd帐户密码 玩家 strUserId这是字符名 sRank角色是否是游戏大师 在中,我需要做的是将登录详细信息与查询中的PHP变量$usernam
- strAccount链接到NGSCUSER的主键,它是 帐户用户名
- strChar01是第一个字符槽
- strChar02是第二个字符槽
- strChar03是第三个字符槽
- strUserID帐户用户名
- strPasswd帐户密码
- strUserId这是字符名
- sRank角色是否是游戏大师
SELECT NGSCUSER.strUserID, NGSCUSER.strPasswd, GAMEUSER.sRank
FROM 'CHARS'
INNER JOIN NGSCUSER ON NGSCUSER.strUserId = CHARS.strAccount
INNER JOIN GAMEUSER ON GAMEUSER.strUserId = CHARS.strChar01
INNER JOIN GAMEUSER ON GAMEUSER.strUserId = CHARS.strChar02
INNER JOIN GAMEUSER ON GAMEUSER.strUserId = CHARS.strChar03
WHERE NGSCUSER.strUserId='$username' and NGSCUSER.strPasswd='$password' and GAMEUSER.sRank = '1'
首先,要真正回答您的问题(这可能无法解决您的问题),您需要为表添加别名以连接多个表:
select * from [chars] c
inner join ngscuser n on n.strUserId = c.strAccount
inner join gameuser g1 on g1.strUserId = c.strChar01
inner join gameuser g2 on g2.strUserId = c.strChar02
inner join gameuser g3 on g3.strUserId = c.strChar03
where n.strUserId='$username' and n.strPasswd='$password' and g1.sRank = '1'
但是,如果您试图撤回与任何角色槽匹配的gameuser
行,则需要使用中的:
select n.strUserID, n.strPasswd, g.sRank
from [chars] c
inner join ngscuser n on n.strUserId = c.strAccount
inner join gameuser g on g.strUserId in (c.strChar01, c.strChar02, c.strChar03)
where n.strUserId='$username' and n.strPasswd='$password' and g.sRank = '1'
现在,请注意,我想做以下公益广告:
我希望您正在清理$username
和$password
以便不受SQL注入的影响
不要以明文形式存储密码。您应该只将它们存储为咸哈希。如果你能做一个选择*from ngscuser
并获得每个人的密码,那你就太丢脸了。羞耻。羞耻。羞耻
应该是CHARS而不是“CHARS”下面是一个示例函数,用于处理您所要求的内容:
function functionName($username,$password){
SELECT tablename.colname AS alias, tablename.colname2 AS alias2, ...
FROM tablename tn
JOIN talbename tn2 on tn2.colname = tn.colname
JOIN anothertable tn3 on tn3.colname = tn2.colname
WHERE tn.colname = $username AND rl.password ='$password'
AND tn3.colname = 1
}
数据库不是我的不幸的是我意识到纯文本密码的危险,sql注入保护是我的下一个任务我注意到mssql没有mysql_real_escape_string();所以我需要一些东西来替换that@arcanine-使用参数化查询。详情如下: