【发布时间】:2019-02-26 10:41:51
【问题描述】:
我使用服务器端在 jqgrid 上创建 json。
我的问题是当我想在 mySQL 查询中包含 WHERE 时,因为 WHERE 已用于搜索运算符。
-
服务器端代码是
<?php include("dbconfig.php"); $page = $_REQUEST['page']; $limit = $_REQUEST['rows']; $sidx = $_REQUEST['sidx']; $sord = $_REQUEST['sord']; // if we not pass at first time index use the first column for the index or what you want if(!$sidx) $sidx =1; //array to translate the search type $ops = array( 'eq'=>'=', //equal 'ne'=>'<>',//not equal 'lt'=>'<', //less than 'le'=>'<=',//less than or equal 'gt'=>'>', //greater than 'ge'=>'>=',//greater than or equal 'nc'=>'NOT LIKE' //doesn't contain ); function getWhereClause($col, $oper, $val){ global $ops; if($oper == 'bw' || $oper == 'bn') $val .= '%'; if($oper == 'ew' || $oper == 'en' ) $val = '%'.$val; if($oper == 'cn' || $oper == 'nc' || $oper == 'in' || $oper == 'ni') $val = '%'.$val.'%'; return " WHERE $col {$ops[$oper]} '$val' "; } $where = ""; //if there is no search request sent by jqgrid, $where should be empty $searchField = isset($_GET['searchField']) ? $_GET['searchField'] : false; $searchOper = isset($_GET['searchOper']) ? $_GET['searchOper']: false; $searchString = isset($_GET['searchString']) ? $_GET['searchString'] : false; if ($_GET['_search'] == 'true') { $where = getWhereClause($searchField,$searchOper,$searchString); // var_dump($where); } $totalrows = isset($_REQUEST['totalrows']) ? $_REQUEST['totalrows']: false; if($totalrows) { $limit = $totalrows; } $db = mysql_connect($dbhost, $dbuser, $dbpassword) or die("Connection Error: " . mysql_error()); mysql_select_db($database) or die("Error connecting to db."); if ($limit<0) $limit = 0; // calculate the number of rows for the query. We need this for paging the result $result = mysql_query("SELECT * FROM enemy_coords"); $row = mysql_fetch_array($result,MYSQL_ASSOC); $count = $row['count']; if( $count >0 ) { $total_pages = ceil($count/$limit); } else { $total_pages = 0; } if ($page > $total_pages) $page=$total_pages; $start = $limit*$page - $limit; // do not put $limit*($page - 1) if ($start<0) $start = 0; // the actual query for the grid data $SQL = "SELECT * FROM enemy_coords ".$where." ORDER BY $sidx $sord LIMIT $start , $limit"; $result = mysql_query( $SQL ) or die("Couldn't execute query.".mysql_error()); $responce->page = $page; $responce->total = $total_pages; $responce->records = $count; $i=0; while($row = mysql_fetch_array($result,MYSQL_ASSOC)) { $responce->rows[$i]['id']=$row[ID]; $responce->rows[$i]['cell']=array($row[ID],$row[ID],$row[alliance_name],$row[player_name],$row[player_lvl],$row[player_might],$row[city1_coords]); $i++; } echo json_encode($responce); ?>
我的问题是我是否将 mysql 查询更改为
SELECT * FROM enemy_coords WHERE category <> 'A',
where should i put the "WHERE category = 'A'"?
【问题讨论】:
标签: jqgrid jqgrid-php