在JQGrid中动态形成查询

时间:2015-03-23 12:17:56

标签: php jquery mysql jqgrid

我想动态查询表中的数据并根据用户登录显示它。我有2个文件:一个是index.php,用于捕获用户的管理产品,如:

$currentUser = CurrentUser::getInstance();
$UserID = $currentUser->getId();

//Get admin products
$que1 = mysql_query("SELECT productid from admin_levels WHERE level>1 AND userid=$UserID ORDER BY productid");
$op1="";

while($row1 = mysql_fetch_row($que1)){
    foreach($row1 as $cell1){
        $op1.=$cell1.",";
    }
}

$temp1=strlen($op1)-1;
$op1=substr($op1,0,$temp1);
//op1 contains admin products for sql command
$_SESSION['w1']=$op1;

op1字符串包含类似于此的内容:

16,17,20,114,125

现在我想在我的jqgrid查询中添加它:

//Get admin products
$y1=$_SESSION['w1'];
$opt1="WHERE issues.productid in (".$y1.")";

//Create query
$grid->SelectCommand = 'SELECT CONCAT(issues.type,"0",issues.kbid) as KBID,issues.issuesummary as Summary,products.productname as Product,issue_priorities.description as Priority,date_format(issues.createddate, "%d/%m/%Y") as Reported,date_format(issues.lastupdated, "%d/%m/%Y") as Updated,issue_status.statusdescription as Status,issue_resolutions.resdescription as Resolution,users.logon as Assigned FROM issues
    INNER JOIN issue_priorities ON issue_priorities.VALUE = issues.PRIORITY - 1 
    INNER JOIN issue_resolutions ON issue_resolutions.RESID = issues.RESOLUTION 
    INNER JOIN users ON users.ID = issues.ASSIGNEDUSERID
    INNER JOIN products ON products.PRODUCTID = issues.PRODUCTID
    INNER JOIN issue_status ON issue_status.STATUSID = issues.STATUS'
    .$opt1;

在回显sql命令时,我得到了这个:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'issues.productid in (16,17,20,114,125) LIMIT 0, 0' at line 6

如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

语法错误可能是单引号试试

 $grid->SelectCommand = "your query".$opt1;

而不是

$grid->SelectCommand = 'your query'.$opt1;