I would like to set an array in an where clause.
function rapport_detail_opbrengsten($idKlant){
$this->db->from('Project');
$this->db->join('Opbrengsten', 'Opbrengsten.idProject = Project.idProject');
if ($idKlant > 0){
$this->db->where('idKlant', $idKlant);}
$query = $this->db->get();
$project = array();
foreach($query->result() as $row){
$project[] = $row->idProject;
}
return implode("`,`", $project);
$this->db->select('idProject, SUM(Prijs) as total');
$this->db->from('Opbrengsten');
$this->db->where_in('idProject', $project);
$this->db->group_by('idProject');
$query = $this->db->get();
if($query->num_rows()>0){
return $query->result();
}
else{
return false;
}
}
The return gives the following: string(66) "7,14,14,81,81,81,14,9,15,11,12,6,6,6,`6"
But somehow this doesnt work in the where_in or the where clause.
you are miss using backticks there you need quotes.Change your where_in() to
$this->db->where_in("idProject",$project);
/* $project should be an array containing list of ids not a string */
According to Active Record in where_in() you can pass array of values in second argument
Or just use where()
$this->db->where("idProject IN('".join("','",$project)."')",FALSE);
Related
I have two list-filtering inputs, if used individually they work perfectly but when used together it returns an empty object.
the filters contain one simple search Input field, and one select.
they trigger an API call to the server which when used both looks like this
http://127.0.0.1:8000/api/user?&search=jom&type=admin
the method which this call trigger looks like this
public function index(Request $request) {
$search = $request->input('search');
$type = $request->input('type');
$user = User::select('*');
$this->checkSearch($user, $search); // check for search
$this->filterUserType($user, $type); // filter user type
...
the method checkSearch looks like this
private function checkSearch(&$query, $search) {
if (!isset($query)) {
return $query;
}
if (!is_null($search)) {
$searchTerms = $this->stringToArray($search, ' ');
$query = $query->where(function ($query) use ($searchTerms) {
for ($i = 0, $max = count($searchTerms); $i < $max; $i++) {
$term = str_replace('_', '\_', mb_strtolower('%' . $searchTerms[$i] . '%'));
$query->whereRaw("(Lower(name) LIKE ?)", [$term, $term])
->orWhereRaw("(Lower(bio) LIKE ?)", [$term, $term]);
}
});
}
}
and the filterUserType like this
private function filterUserType(&$query, $type) {
if (!isset($query)) { return $query; }
if (!is_null($type)) {
$query = $query->where( 'type', $type);
}
}
I've tried to check the where on the filterUserType method, to orWhere but this just returns values of both not combined.
I triggered a break on the raw query and it appeared like this
select * from `users` where ((Lower(name) LIKE %jom%) or (Lower(bio) LIKE %jom%)) and `type` = %jom%)
When I switched the methods I got the right results.
Switching from
$this->checkSearch($user, $search); // check for search
$this->filterUserType($user, $type); // filter user type
to
$this->filterUserType($user, $type); // filter user type
$this->checkSearch($user, $search); // check for search
Still don't know why but it worked
I need to get all the rows which are filters by the query. But only the first row is returned from the model to the controller.
Given below is my model function. How to get all the data needed?
public function getOfferTags($param) {
$this->db->select('tags.*');
$this->db->from('tags');
$this->db->join('offer_tag', 'offer_tag.tag_id = tags.id');
$this->db->join('offers', 'offers.id = offer_tag.offer_id');
$this->db->where('offers.id', $param);
$query = $this->db->get();
return $query->row();
}
Simply not return row and on that place fetch as an array form like
return $query->result_array();
After that checks in controller and get the result in array.
I just wanted to add, because you are new to CI, that you should always check if their are rows before using the array to prevent notices or issues. with mehta's method you would do if(count($rows) > 0) { //rows exist } else { // no rows, display error .etc. } or you can do:
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result_array(); // or result() for obj
}
return false;
Usage:
if ($this->somemodel->getOfferTags($stmt)) {
// has data
} else {
// no data
}
It is important to start with good practices and this will help ;)
I'm trying to show the name of table in my database. I write this code :
function affiche_liste()
{
$db=new PDO('mysql:host=localhost;dbname=testf','root','');
$result = $db->query("SHOW TABLES");
foreach($result->fetch(PDO::FETCH_NUM) as $data) {
$tableList = $data[0];
}
return $tableList;
}
It give to me only the last table ?
For a simple query without parameters and the SQL hard coded you can use a generic function passing the connection and SQL to the function.
The following function () returns an array containing all rows in the result set.
function queryAll($db,$query){
$sth = $db->query($query);
$result = $sth->fetchAll(PDO::FETCH_NUM);
return $result;
}
For a simple query without parameters and the SQL hard coded you can use a generic function passing the connection and SQL to the function.
The following function () returns an array containing all rows in the result set.
function queryAll($db,$query){
$sth = $db->query($query);
$result = $sth->fetchAll(PDO::FETCH_NUM);
return $result;
}
$db=new PDO('mysql:host=localhost;dbname=testf','root','');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = "SHOW TABLES";
$tables = queryAll($db,$query);
print_r($tables);
Each time you are looping through the results, your are overwriting the variable tableList. Instead, you need to append to an array of results.
function affiche_liste()
{
$db=new PDO('mysql:host=localhost;dbname=testf','root','');
$result = $db->query("SHOW TABLES");
$tableList = array();
foreach($result->fetch(PDO::FETCH_NUM) as $data) {
array_push($tableList, $data[0]);
}
return $tableList;
}
try this
`
function affiche_liste()
{$db=new PDO('mysql:host=localhost;dbname=testf','root','');
$result = $db->query("SHOW TABLES");
foreach($result->fetch(PDO::FETCH_NUM) as $data) {
$tableList[] = $data[0];
}
return $tableList;
}
`
i hope it'll work...
here your array is overwitting every time...that's the reason you were getting the last table name....so you need to append to the existing array...
I have written a function that will query the database. The sql statement includes a where clause. However, I keep getting this error
"Message: odbc_exec(): SQL error: [Microsoft][ODBC SQL Server
Driver][SQL Server]Invalid column name 'home'., SQL state S0022 in
SQLExecDirect".
The column name should be banner_category while "home_banner) is the value.
How should I go about achieving it?
public function get_landing_banners()
{
$query = $this->db->query(
'SELECT *
FROM o2o_banner
WHERE banner_category='home_banner'');
$data = array();
foreach ($query->result_array() as $row)
{
$data[] = $row;
}
return $data;
}
If you want to return an array try this:
public function get_landing_banners()
{
$this->db->select('*')->from('o2o_banner')->where('banner_category', 'home_banner');
$q = $this->db->get();
if ($q->num_rows() > 0) {
return $q->result_array();
}
}
Try this coding
public function get_landing_banners()
{
$query = $this->db->query(
'SELECT *
FROM o2o_banner
WHERE banner_category="home_banner"');
$data = array();
foreach ($query->result_array() as $row)
{
$data[] = $row;
}
return $data;
}
When I execute the function below query execute successfully but I get a warning above the result the query:
mysql_num_rows() expects parameter 1 to be resource, boolean
How do I fix this?
public function retrieve()
{
$id=JRequest::getVar('id');
$db =JFactory::getDBO();
$sql="select *
from
#__npco_car,#__npco_namayeshgah,#__npco_agahi
where
#__npco_car.car_id='$id' and
#__npco_namayeshgah.id_namayeshgah=#__npco_agahi.id_namayeshgah and
#__npco_car.car_id=#__npco_agahi.car_id
";
$db->setQuery($sql);
$db->query();
$row = $db->getNumRows();
if($row == 1) {
return $db->loadAssocList();
} else {
$db = JFactory::getDBO();
$sql="select *
from
#__npco_car,#__npco_useragahi,#__npco_user
where
#__npco_car.car_id='$id' and
#__npco_user.id_user=#__npco_useragahi.id_user and
#__npco_car.car_id=#__npco_useragahi.car_id
";
$db->setQuery($sql);
return $db->loadAssocList();
}
}
Your code has several issues.
Never use unchecked/unvalidated request values, not even in examples!
Use the query builder.
Reduce coupling by a) setting the database in the constructor, which is done already in models, and b) retrieve the id in the controller.
You try to get all fields (*) from multiple tables, which have some column names in common. That will not work.
Have a look at JOINs.
This will work:
public function retrieve($id)
{
$query = $this->_db->getQuery(true);
$query->select('#__npco_car.*')->from(array('#__npco_car', '#__npco_namayeshgah', '#__npco_agahi'));
$query->where('#__npco_car.car_id = ' . (int) $id);
$query->where('#__npco_namayeshgah.id_namayeshgah = #__npco_agahi.id_namayeshgah');
$query->where('#__npco_car.car_id = #__npco_agahi.car_id');
$this->_db->setQuery($sql);
$rows = $this->_db->loadAssocList();
if (empty($rows))
{
$query = $this->_db->getQuery(true);
$query->select('#__npco_car.*')->from(array('#__npco_car, #__npco_useragahi, #__npco_user'));
$query->where('#__npco_car.car_id = ' . (int) $id);
$query->where('#__npco_user.id_user = #__npco_useragahi.id_user');
$query->where('#__npco_car.car_id = #__npco_useragahi.car_id');
$db->setQuery($sql);
$this->_db->setQuery($sql);
$rows = $this->_db->loadAssocList();
}
return $rows;
}
may be this is your issue..
Change your query as following
$sql="select *
from
#__npco_car,#__npco_namayeshgah,#__npco_agahi
where
#__npco_car.car_id='".$id."' and
#__npco_namayeshgah.id_namayeshgah=#__npco_agahi.id_namayeshgah and
#__npco_car.car_id=#__npco_agahi.car_id
";
$sql="select *
from
#__npco_car,#__npco_useragahi,#__npco_user
where
#__npco_car.car_id='".$id."' and
#__npco_user.id_user=#__npco_useragahi.id_user and
#__npco_car.car_id=#__npco_useragahi.car_id
";