我需要使用多变量进行搜索并排序

发布于 2024-11-29 01:32:17 字数 1283 浏览 1 评论 0原文

我可以使用 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 为资源,布尔值在 /Applications/XAMPP/xamppfiles/htdocs/work/mobjob/test.php 第 41 行给出

I can search multiple value with mysql.
I need to implement SORT BY on it

This is my coding which is working perfect

   $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);

When I apply SORT BY
like this

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);
}

result is

Blockquote
SELECT * FROM jobs JOIN job_category ON jobs.job_category=job_category.category_id job_location = "Manjeri" AND ORDER BY "job_salary"
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/work/mobjob/test.php on line 41

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

全部不再 2024-12-06 01:32:17

你不应该将你的订单作为一个条件..它不是一个..

只需将其附加到末尾,如下所示:

$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.'"';
}

you shouldn't implode your order by as a condition.. it's not one..

just append it onto the end like so:

$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.'"';
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文