我需要搜索多变量和排序

时间:2011-08-08 19:25:59

标签: php mysql mysql-error-1064 mysql-error-1054

我可以用mysql搜索多个值。 我需要在它上面实现SORT BY

这是我的编码工作正常

   $conditions = array();
if ($key) {
  $conditions[] = 'job_title LIKE "%'.$key.'%"';
}
if ($category) {
  $conditions[] = 'job_category = "'.$category.'"';
}
if ($location) {
  $conditions[] = 'job_location = "'.$location.'"';
}
if ($country) {
  $conditions[] = 'job_country = "'.$country.'"';
}
if ($salary) {
  $conditions[] = 'job_salary >= "'.$salary.'"';
}


$sqlStatement = 'SELECT * FROM jobs JOIN job_category ON  jobs.job_category=job_category.category_id  '.implode(' AND ', $conditions);

当我申请SORT BY时 像这样

if ($sort) 
{
$conditions[] = 'ORDER BY "'.$sort.'"';
$sqlStatement = 'SELECT * FROM jobs JOIN job_category ON  jobs.job_category=job_category.category_id  '.implode(' AND ', $conditions);
}
else
{
$sqlStatement = 'SELECT * FROM jobs JOIN job_category ON  jobs.job_category=job_category.category_id  '.implode(' AND ', $conditions);
}

结果是

  

块引用   SELECT * FROM jobs JOIN job_category ON jobs.job_category = job_category.category_id job_location =“Manjeri”AND ORDER BY“job_salary”   警告:mysql_fetch_array()要求参数1为资源,第41行/Applications/XAMPP/xamppfiles/htdocs/work/mobjob/test.php中给出布尔值

1 个答案:

答案 0 :(得分:3)

你不应该因为一个条件而破坏你的订单..它不是一个......

只需将其附加到最后:

$sqlStatement = 'SELECT * FROM jobs JOIN job_category ON  jobs.job_category=job_category.category_id  ';

if( sizeof($conditions)) {
    $sqlStatement .= ' WHERE ' . implode(' AND ', $conditions);
}



if($sort) {
    $sqlStatement .= ' ORDER BY "'.$sort.'"';
}