mysql查询问题,编码错误?
我正在尝试执行以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
很可疑,应该
确保 $N 没有任何邪恶之处。
is fishy, should be
make sure, $N has nothing evil in it.
不确定这是否只是你在 stackoverflow 中的一个糟糕的复制/粘贴工作,但看起来你的大括号都出了问题。认为您会想要更像这样的东西:
以便您的 foreach 循环实际上执行其下面的代码。您粘贴到问题中的代码有一个 for every 循环,它在循环中不执行任何内容,因为它是这样写的:
然后您继续尝试使用循环外的 foreach 循环中的变量之一:
这不会之所以有效,是因为
$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:
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:
And then you proceed to try to use one of the variables from the foreach loops outside of the loop:
Which won't work because the
$groupName
variable will only be set within the scope of theforeach
loop.