我是PHP和Codeigniter的新手,我正在开发一个小型的婚姻网站应用程序。
事实是,我有一个用简单的PHP编写的SQL查询,现在我正在寻找的是与Codeigniter风格相同的SQL查询。
以下是普通的SQL查询,需要以Codeigniter样式编写。
SELECT
*
FROM
users
WHERE
IF('$se_ct' != '',
sect = '$se_ct'
AND IF('$subsect' != '',
subsect = '$subsect',
subsect LIKE '%%'),
sect LIKE '%%' AND subsect LIKE '%%')
AND IF('$coun_try' != '',
country = '$coun_try'
AND IF('$sta_te' != '',
state = '$sta_te'
AND IF('$ci_ty' != '',
city = '$ci_ty',
city LIKE '%%'),
state LIKE '%%' AND city LIKE '%%'),
country LIKE '%%' AND state LIKE '%%'
AND city LIKE '%%')
AND age >= '$age_from'
AND age <= '$age_to'
AND IF('$qualification' != '',
qualification = '$qualification',
qualification LIKE '%%')
AND gender = '$look'
AND status = '1'
感谢帮助者,指导者和开发者。
答案 0 :(得分:0)
你也可以在codeigniter
中使用它 $this->db->query("SELECT *
FROM users
WHERE
if('$se_ct'!='',sect = '$se_ct'
AND
if('$subsect' !='',subsect = '$subsect',subsect like '%%'),
sect like '%%' AND subsect like '%%')
AND
IF( '$coun_try' !='', country = '$coun_try'
AND
if('$sta_te' !='', state = '$sta_te'
AND
if('$ci_ty' !='',city = '$ci_ty',city like '%%'),state LIKE '%%'
AND city LIKE '%%'), country LIKE '%%'
AND state LIKE '%%'
AND city LIKE '%%' )
AND age >= '$age_from'
AND age <= '$age_to'
AND
IF('$qualification' !='',qualification = '$qualification', qualification LIKE '%%' )
AND gender = '$look'
And status='1'");
//for echo the last executed query
$this->db->last_query();
//for returning the result from model to controller
return $query->result_array();
答案 1 :(得分:0)
您可以使用这些纯Codeigniter: -
Codeigniter模型代码: -
function get_result()
{
$this->db->select('*');
if ($se_ct != "")
{
$this->db->where('sect', $se_ct);
if ($subsect != "")
{
$this->db->where('subsect', $subsect);
}
else
{
$this->db->like('subsect', "");
}
}
if($coun_try != "")
{
$this->db->where('coutry', $count_try);
if ($sta_te != "")
{
$this->db->where('state', $sta_te);
if ($ci_ty != "")
{
$this->db->where('city', $ci_ty);
}
else
{
$this->db->like('city', "");
}
}
else
{
$this->db->like('state', "");
}
}
else
{
$this->db->like('country', "");
}
$this->db->where(array('age >=' => $age_from, 'age <=' => $age_to));
if ($qualification != "")
{
$this->db->where('qualification', $qualification);
}
else
{
$this->db->like('qualification', "");
}
$this->db->where('gender', $look);
$this->db->where('status', 1);
$this->db->from('users');
return $this->db->get();
}
Codeigniter控制器的代码: -
$data['user'] = $this->user_model->get_result(); // take model as per your code
$this->load->view('user', $data); // take view file as per your code
答案 2 :(得分:0)
您可以按如下方式编写查询
return $this->db->query("SELECT * FROM users
WHERE
if('$se_ct'!='',sect = '$se_ct'
AND
if('$subsect' !='',subsect = '$subsect',subsect like '%%'),
sect like '%%' AND subsect like '%%')
AND
IF( '$coun_try' !='', country = '$coun_try'
AND
if('$sta_te' !='', state = '$sta_te'
AND
if('$ci_ty' !='',city = '$ci_ty',city like '%%'),state LIKE '%%'
AND city LIKE '%%'), country LIKE '%%'
AND state LIKE '%%'
AND city LIKE '%%' )
AND age >= '$age_from'
AND age <= '$age_to'
AND
IF('$qualification' !='',qualification = '$qualification', qualification LIKE '%%' )
AND gender = '$look'
And status='1'")->result();