我怎样才能使这个 mysql 查询工作而不得到空结果?

发布于 2024-11-29 16:33:46 字数 1498 浏览 0 评论 0原文

我无法从这个 mysql 查询中获取我想要的结果。我认为问题在于这行代码:

$group="SELECT * from mybb_users where usergroup AND extragroups = '$usergroup'";

因为我注意到数据库中的additionalgroups列有多个用逗号(,)分隔的值,而usergroup只有一个值。这是一个屏幕截图:

这是一个图像: https://i.sstatic.net/HVVN9.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
{
}
}

}

I am having trouble getting teh results i want from this mysql query. And i think the trouble lies in this line of code:

$group="SELECT * from mybb_users where usergroup AND additionalgroups = '$usergroup'";

because i noticed that the additionalgroups column in the database has more than one value separated by a comma(,) whereas the usergroup only has ONE value. Here is a screenshot:

here is an image: https://i.sstatic.net/HVVN9.jpg

The entire code works perfect if i remove the additionalgroups column from the code and only check the usergroup column, but that is not what i want :( Below is the entire code:

// 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
{
}
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紅太極 2024-12-06 16:33:46

一种检查方法是使用 FIND_IN_SET 函数。

SELECT * from mybb_users where usergroup AND FIND_IN_SET('$usergroup', additionalgroups) != 0

On way to check is with the FIND_IN_SET function.

SELECT * from mybb_users where usergroup AND FIND_IN_SET('$usergroup', additionalgroups) != 0
方圜几里 2024-12-06 16:33:46

我不确定 where usergroup AND ... - usergroup 是数据库字段吗?不管怎样,你可以用

$group="SELECT * from mybb_users where usergroup AND FIND_IN_SET('$usergroup', additionalgroups) != 0;

但这还不够。 AFAICS 您很容易受到 SQL 注入攻击。请在正确的位置使用mysql_real_escape(),即在将带有用户输入的变量放入 SQL 查询的任何位置。

I'm not sure about where usergroup AND ... - is usergroup a DB field? Anyway, you could do it with

$group="SELECT * from mybb_users where usergroup AND FIND_IN_SET('$usergroup', additionalgroups) != 0;

But that is not enough yet. AFAICS you are vulnerable to SQL injection. Please use mysql_real_escape() at the right places, namely everywhere you put variables with user input into a SQL query.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文