mysql查询问题,编码错误?

时间:2011-12-28 23:21:19

标签: php mysql select where-clause

我正在尝试执行以下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");

当我像其他人一样登录时,我得到了:

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

2 个答案:

答案 0 :(得分:0)

$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没有任何邪恶。

答案 1 :(得分:0)

不确定这是否只是一个糟糕的复制/粘贴作业,你进入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循环,它在循环中不执行任何操作,因为它是这样编写的:

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

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

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

哪个不起作用,因为$groupName变量只会在foreach循环的范围内设置。