在Codeigniter中的模型中运行多个查询 [英] Running multiple queries in model in codeigniter
本文介绍了在Codeigniter中的模型中运行多个查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在codeigniter的模型中有以下代码:
I have this code in model in codeigniter:
<?php
Class Mymodel Extends CI_Model
{
function __construct()
{
parent::__construct();
}
function search($textinput)
{
$street = "select street from dataSearch;";
$stripNameWOSpace = "select stripNameWOSpace FROM dataSearch;";
$vowels = array('a','e','i','o','u',';','/','-', ' ');
$string = strtolower($textinput);
$strippedVowels = mysql_real_escape_string(str_replace($vowels, '', $string));
$this->db->query("select dataSearch.id,
dataSearch.name,
dataSearch.street,
dataSearch.city,
dataSearch.lat,
dataSearch.lng,
category.asiatype from dataSearch join category on dataSearch.cat = category.id
where dataSearch.street like '%".$textinput."%'");
$this->db->query("select dataSearch.id,
dataSearch.name,
dataSearch.street,
dataSearch.city,
dataSearch.lat,
dataSearch.lng,
category.asiatype from dataSearch join category on dataSearch.cat = category.id
where dataSearch.stripNameWOSpace like '%".$strippedVowels."%'");
$query = $this->db->get();
$query->result();
}
}
?>
我只想执行多个查询。您会在where语句中注意到两者具有不同的条件。我只想获取两个查询的结果。
我尝试使用switch语句执行两个查询,但它不起作用。帮帮我。
I just want to execute multiple queries. You notice in where statement the two have different condition. I just want to get the result for the two queries. I try the switch statement to execute both queries and it is not working. Help me.
推荐答案
您可以将查询设置为变量,以便对每个查询执行操作。
You can set the query to a variable to do your things with each query.
就像这样:
Class Mymodel Extends CI_Model
{
function __construct()
{
parent::__construct();
}
function search($textinput) {
$query1 = $this->db->query("YOUR QUERY");
$query2 = $this->db->query("YOUR SECOND QUERY");
$result1 = $query1->result();
$result2 = $query2->result();
return array_merge($result1, $result2); // If you want to merge both results
}
}
在您的控制器:
$this->load->model('Mymodel');
$searchresult = $this->Mymodel->search($textinput);
有关更多信息,您应该阅读 CodeIgniter用户指南-模型
For more information, you should read CodeIgniter User Guide - Model
这篇关于在Codeigniter中的模型中运行多个查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文