mysql查询问题,编码错误?

发布于 2024-12-23 09:14:43 字数 1019 浏览 2 评论 0原文

我正在尝试执行以下 PHP / MySQL 查询,对于前两组来说它工作正常,但对于其他人我收到 MySQL 错误,这写得正确吗?

$user =& JFactory::getUser();
$N = $user->get('name'); 
$username = $user->get('username');
$groups = $user->get('groups');

foreach($groups as $groupName=>$groupId)
{
}

$G=$groupName;

if ($G=="Management Staff")
    $result = mysql_query("SELECT * FROM hqfjt_chronoforms_data_addupdatelead");
elseif ($G=="Website Developers")
    $result = mysql_query("SELECT * FROM hqfjt_chronoforms_data_addupdatelead");
else
    $result = mysql_query("SELECT * FROM hqfjt_chronoforms_data_addupdatelead WHERE createdby=$N");

当我以其他人身份登录时,我得到:

警告:mysql_fetch_object():提供的参数不是 C:\server2go\server2go\htdocs\chandlers\components\com_jumi\views\application\view.html 中的有效 MySQL 结果资源.php(38) : 第 87 行 eval() 代码

警告:mysql_free_result() 期望参数 1 为资源, C:\server2go\server2go\htdocs\chandlers\components\com_jumi\views\application\view.html.php(38) 中给出的布尔值:第 132 行的 eval() 代码

I am trying to do the following PHP / MySQL query, and it works fine for the first two groups, but for everyone else I am getting a MySQL error, is this written correctly?

$user =& JFactory::getUser();
$N = $user->get('name'); 
$username = $user->get('username');
$groups = $user->get('groups');

foreach($groups as $groupName=>$groupId)
{
}

$G=$groupName;

if ($G=="Management Staff")
    $result = mysql_query("SELECT * FROM hqfjt_chronoforms_data_addupdatelead");
elseif ($G=="Website Developers")
    $result = mysql_query("SELECT * FROM hqfjt_chronoforms_data_addupdatelead");
else
    $result = mysql_query("SELECT * FROM hqfjt_chronoforms_data_addupdatelead WHERE createdby=$N");

When I login as anyone else I get :

Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in C:\server2go\server2go\htdocs\chandlers\components\com_jumi\views\application\view.html.php(38) : eval()'d code on line 87

Warning: mysql_free_result() expects parameter 1 to be resource, boolean given in C:\server2go\server2go\htdocs\chandlers\components\com_jumi\views\application\view.html.php(38) : eval()'d code on line 132

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

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

发布评论

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

评论(2

总以为 2024-12-30 09:14:43
$result = mysql_query("SELECT * FROM hqfjt_chronoforms_data_addupdatelead WHERE createdby=$N");

很可疑,应该

$result = mysql_query("SELECT * FROM hqfjt_chronoforms_data_addupdatelead WHERE createdby='$N'");

确保 $N 没有任何邪恶之处。

$result = mysql_query("SELECT * FROM hqfjt_chronoforms_data_addupdatelead WHERE createdby=$N");

is fishy, should be

$result = mysql_query("SELECT * FROM hqfjt_chronoforms_data_addupdatelead WHERE createdby='$N'");

make sure, $N has nothing evil in it.

尸血腥色 2024-12-30 09:14:43

不确定这是否只是你在 stackoverflow 中的一个糟糕的复制/粘贴工作,但看起来你的大括号都出了问题。认为您会想要更像这样的东西:

$username = $user->get('username');
$groups = $user->get('groups');
foreach($groups as $groupName=>$groupId) {
  $G=$groupName;
  if ($G=="Management Staff")
    $result = mysql_query("SELECT * FROM hqfjt_chronoforms_data_addupdatelead");
  elseif ($G=="Website Developers")
    $result = mysql_query("SELECT * FROM hqfjt_chronoforms_data_addupdatelead");
  else
    $result = mysql_query("SELECT * FROM hqfjt_chronoforms_data_addupdatelead WHERE     createdby=$N");
}

以便您的 foreach 循环实际上执行其下面的代码。您粘贴到问题中的代码有一个 for every 循环,它在循环中不执行任何内容,因为它是这样写的:

foreach($groups as $groupName=>$groupId) {}

然后您继续尝试使用循环外的 foreach 循环中的变量之一:

$G=$groupName;
if ($G=="Management Staff")
...

这不会之所以有效,是因为 $groupName 变量只能在 foreach 循环的范围内设置。

Not sure if this was just a poor copy/paste job by you into stackoverflow but it looks like your curly braces are all out of sorts. Think you would want something more like this:

$username = $user->get('username');
$groups = $user->get('groups');
foreach($groups as $groupName=>$groupId) {
  $G=$groupName;
  if ($G=="Management Staff")
    $result = mysql_query("SELECT * FROM hqfjt_chronoforms_data_addupdatelead");
  elseif ($G=="Website Developers")
    $result = mysql_query("SELECT * FROM hqfjt_chronoforms_data_addupdatelead");
  else
    $result = mysql_query("SELECT * FROM hqfjt_chronoforms_data_addupdatelead WHERE     createdby=$N");
}

So that your foreach loop actually executes the code below it. The code you pasted into your question has a for each loop which executes nothing in the loop because it is written like this:

foreach($groups as $groupName=>$groupId) {}

And then you proceed to try to use one of the variables from the foreach loops outside of the loop:

$G=$groupName;
if ($G=="Management Staff")
...

Which won't work because the $groupName variable will only be set within the scope of the foreach loop.

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