我无法从这个 mysql 查询中获得我想要的结果。我认为问题在于这行代码:
$group="SELECT * from mybb_users where usergroup AND additionalgroups = '$usergroup'";
因为我注意到数据库中的 additionalgroups 列有多个值,用逗号 (,) 分隔,而 usergroup 只有一个值。这是一个屏幕截图:
这是一张图片:http://i.imgur.com/UTbmX.jpg
如果我从代码中删除 additionalgroups 列并只检查 usergroup 列,整个代码工作完美,但这不是我想要的 :( 下面是整个代码:
// Connect to server and select databse.
mysql_connect("$db_host", "$db_user", "$db_pass")or die("cannot connect to mysql");
mysql_select_db("$db_name")or die("cannot select DB");
$id=$_GET['lid']; // Get lid from URL
$usergroup =$_GET['game']; // Get the usergroup/game from the URL
// Check to see if the user is a VIP Member and fetch them.
$group="SELECT * from mybb_users where usergroup AND additionalgroups = '$usergroup'";
$group2=mysql_query($group) or die("Could not get users");
while($raw=mysql_fetch_array($group2))
{
// Fetch all UserIDs of the VIP members and match them with the UfID (in the userfields table)
$userid = $raw['uid'];
$group3="SELECT * from mybb_userfields where ufid = '$userid'";
$group4=mysql_query($group3) or die("Could not match userid");
while($raw=mysql_fetch_array($group4))
{
// assigns a lid from the vip members to the variable $lid
$lid = $raw['fid7'];
// Display the hash of the lid if it matches with the lid from the URL
if($lid == '')
{
}
elseif($lid == $id)
{
echo "[key]{$lid};";
}
else
{
}
}
}
最佳答案
检查方法是使用 FIND_IN_SET 函数。
SELECT * from mybb_users where usergroup AND FIND_IN_SET('$usergroup', additionalgroups) != 0
关于php - 我怎样才能使这个 mysql 查询工作而不会得到空结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7060162/