I am working on codeigniter project, I need to migrate whole database mysql to sql server, for that i am getting issue in select query, I can see in sql server round brackets are not supported around the table name, here is my codeigniter query
$this->db->_protect_identifiers=false;
$this->db->select('*')->from('tb_card',false);
$this->db->where('company_id',$this->company_id,FALSE)->get()->row_array();
This select query generates below query
SELECT * FROM (tb_card) WHERE company_id = 27
You can see there is round brackets around the table name, i want to remove this, can anyone please help me to resolve this issue ?
Finally got the solution
(/system/database/drivers/odbc/odbc_driver.php
In this path need to change the function like this way
function _from_tables($tables)
{
/*if ( ! is_array($tables))
{
$tables = array($tables);
}
return '('.implode(', ', $tables).')';*/
if ( ! is_array($tables))
{
return strstr($tables, ',') ? '('.$tables.')' : $tables;
}
else
{
return count($tables) > 1 ? '('.implode(', ', $tables).')' : end($tables);
}
}
Can you able use simple $this->db->query($sql) method, instead of Active Record.
$result = $this->db->query('SELECT * FROM tb_card WHERE company_id = 27');
printr($this->db->last_query());
printr($result->result());
you can use $this->db->get(); if you want select * (all)
for example, is something like....
function getcompany($company_id)
{
$this->db->where('company_id',$company_id);
$query = $this->db->get('tb_card');
return $query->result();
}
those function will return like so
SELECT * FROM 'tb_card' WHERE 'company_id' = $company_id
hope its answer your question.
please use this arcsecond symbol in table name tb_card
$this->db->_protect_identifiers=false; $this->db->select('*')->from('`tb_card`',false); $this->db->where('company_id',$this->company_id,FALSE)->get()->row_array();
Related
I am trying to filter a database by having a category column, so I search for the category and the correct items are returned. However some items have more than one category so I have used comma separated values to put two or more categories in one column. I tried this method:
public function get_cat_article($value)
{
$table = $this->get_table();
$this->db->where('cat', $value);
return $this->db->get($table);
}
but that only picks up the first of the separated values. I tried using 'where_in' but that also picked up only the first value. I tried 'like' and that worked but was not definitive. A search for 'WS' included other categories like 'IWS'. I googled for a solution and fin_in_set was suggested so I tried:
public function get_cat_article($value)
{
$table = $this->get_table();
$this->db->where('find_in_set('.$value.',cat)');
return $this->db->get($table);
}
But this didn't work at all. Have I got the syntax wrong or is this the wrong approach?
Have you tried like query? In a quick demo, this works for me.
public function get_cat_article($value)
{
$table = $this->get_table();
$this->db->like('cat', $value); // say, $value = 'reebok'
return $this->db->get($table);
// Produces: SELECT * FROM $table WHERE `cat` LIKE '%reebok%' ESCAPE '!'
}
For more details read CodeIgniter official user guide here.
Try like method but this way:
public function get_cat_article($value)
{
$table = $this->get_table();
$this->db->like('cat', $value . ',', 'after');
$this->db->or_like('cat', ',' . $value . ',');
$this->db->or_like('cat', ',' . $value, 'before');
return $this->db->get($table);
}
The problem appears to be solved using the following line of code using the MySql 'find_in_set' technique. I got the code from this website at Check the conditions in comma separated values
and the key line is:
$this->db->where("FIND_IN_SET( '$post_period' , period) ");
I just got the syntax wrong.
I am trying to retrieve the config_value on the basis of config_name from the table as shown in image in codeigniter project. Even i am not able to understand where to start.I find something like this on internet (for Sample).
$this->db->select('age');
$this->db->where('id', '3');
$q = $this->db->get('my_users_table');
$data = $q->result_array();
echo($data[0]['age']);
Do something like this :
$this->db->where('config_name', 'Account_activation');
$q = $this->db->get('my_users_table');
/* if u r fetching one row use row_array instead of result_array*/
$row = $q->row_array();
echo $row['config_value'];
For more : https://www.codeigniter.com/user_guide/database/index.html
If you want to retrieve config value on the basis of config name you can simply add an where condition in select query just like I am giving here, put whatever config_name you want to retrieve the query and it will print the config value which is there in the same row.
$this->db->select('config_value');
$q = $this->db->get_where('my_users_table',array('config_name'=>'home_page'));
$row = $q->row_array();
echo $row['config_name']; // this will print index.php
Little different than the other answers although functionally the same. You could make a helper and include this function. Really only useful if you only want to get one config item at a time.
function config_item_db($key) {
$CI = & get_instance();
$query = $CI->db->get_where('my_users_table', array('config_name' => $key));
if ($query->num_rows() == 1) {
return $query->row()->{$key};
} else {
return false;
}
}
Use codeignitor's get_where() method and pass the select condtions in an array. Example:
$this->load->database();
$this->db->get_where('table_name', array('database_column_title' => $value1,
'database_column_title' => $value2));
I've got a table in which a field contains pattern Like this [{"vendor":"10","status":"paid"}] :
table
I want to make a query 'like' in codeigniter , but I got an error:
model :
function get_total_order($id_vendor){
$this->db->like('payment_status', 'vendor":"'.$id_vendor.'","status":"due');
$this->db->from('sale');
return $this->db->count_all_results();
}
view :
<?php
$new_order = $this->crud_model->get_total_order($this->session->userdata('vendor_id'));
echo "<h1>".$new_order."</h1>";
?>
when i run this, i got blank page, how i fix this?
thanks.
Since you use "Like" query type, you should add '%' in the query argument or send a complete argument:
function get_total_order($id_vendor)
{
$this->db->like('payment_status', '%vendor":"'.$id_vendor.'","status":"due%');
$this->db->from('sale');
return $this->db->count_all_results();
}
Try this:
function get_total_order($id_vendor){
$this->db->like('vendor',$id_vendor);
$this->db->like('status',"due");
$this->db->from('sale');
return $this->db->count_all_results();
}
if your searching json data so you have pass the data in like query and like query data should be look like data inside the table how it looks .
your query should be something like this
<?php
$id_vendor =123;
$ss= '%"vendor":"'.$id_vendor.'","status":"due"%';
$sssss ="select * from sale where payment_status like '$ss' ";
echo $sssss;
query look like this
select * from sale where payment_status like '%"vendor":"123","status":"due"%'
?>
and also you can use wildcard (%) more place with your wish.
You can customize where as per your requirement with and condition or another condition.
$where = "payment_status like '%$id_vendor%' OR status like '%$status%'";
$this->db->where($where);
try this one:
Because 'like is time consuming.
function get_total_order($id,$vendor)
{
$this->db->where('vender', $id);
$this->db->where('status',$vendor);
$this->db->get('sale');
$result=$res->result_array();
return $result;
}
You can use $this->db->where_in() like below:-
$names = array('Frank', 'Todd', 'James');
$this->db->where_in('username', $names);
// Produces: WHERE username IN ('Frank', 'Todd', 'James')
For more details, please check below link:-
https://www.codeigniter.com/userguide2/database/active_record.html
I have a MySQL query which is this:
SELECT * FROM tbl_post WHERE tbl_post.post_id NOT IN
(SELECT tbl_readsave.post_id FROM tbl_readsave)
I want to convert it into Codeigniter Active records, so I used the following code segment:
$this->db->select('tbl_readsave.post_id');
$queryReadSave = $this->db->get('readsave');
$this->db->where_not_in('post_id', $queryReadSave->result_array());
$queryNewPost = $this->db->get('readsave');
if($queryNewPost->num_rows()>0)
{
return $queryNewPost->result_array();
}
else
return false;
However, the code throws me an error, which is like the following:-
Error Number: 1054
Unknown column 'Array' in 'where clause'
SELECT * FROM (`tbl_readsave`) WHERE `post_id` NOT IN (Array)
Filename: /var/www/html/teamF/tharjumal/models/webservice_model.php
Line Number: 28
How can I convert the above stated query into Codeigniter Active Records format?
$queryReadSave->result_array() returns an array of arrays. You can't use that in where_not_in.
You need to loop over that and create a flat array of the IDs you (don't) want.
$this->db->select('post_id');
$queryReadSave = $this->db->get('readsave');
$postIDs = $queryReadSave->result_array();
$this->db->where_not_in('post_id', array_column($postIDs, 'post_id'));
$queryNewPost = $this->db->get('post');
array_column() only exists in PHP 5.5+. If you are on a lower version, you'll need to do something like this:
$this->db->select('post_id');
$queryReadSave = $this->db->get('readsave');
$postIDs = array_map(function($a){
return $a['post_id'];
}, $queryReadSave->result_array());
$this->db->where_not_in('post_id', $postIDs);
$queryNewPost = $this->db->get('post');
P.S. Your second table is called post, right? You'll need to update the query to use the right table.
Update: Your original query uses a subquery which is not natively supported by CodeIgniter. If you want to try this all as one query, you can use the Subquery library I created (https://github.com/NTICompass/CodeIgniter-Subqueries).
$this->db->select('*');
$this->db->from('post');
$sub = $this->subquery->start_subquery('where_in');
$sub->select('post_id')->from('readsave');
$this->subquery->end_subquery('post_id', FALSE);
$queryNewPost = $this->db->get();
try below code:
$read_post_id = [];
$queryReadSave = $this->db->select('post_id')->get('tbl_readsave')->result_array();
if(count($queryReadSave) > 0){
foreach($queryReadSave as $row){
$read_post_id[] = $row['post_id']; // add each post id to the array
}
}
$this->db->select('*');
if(!empty($read_post_id)) $this->db->where_not_in('post_id',$read_post_id);
$post = $this->db->get('tbl_post');
print_r($post->result_array());
exit;
I'm trying to execute SQL query with table gateway which contain COUNT(*) expression in ZF2. This is function in my model:
public function brKomentariUred(){
$sql = $this->tableGateway->getSql();
$select = $sql->select();
$select->columns(array('brKomentari' => new \Zend\Db\Sql\Expression('count(komentarID)'), 'uredId' => 'ured'));
$select->group('ured');
//echo $sql->getSqlStringForSqlObject($select); die();
return $this->tableGateway->selectWith($select);
}
When the query is printed it is correct
SELECT count(komentarID) AS `brKomentari`, `komentar`.`ured` AS `uredId` FROM `komentar` GROUP BY `ured`
In the controller I'm trying to call the query with this code
foreach($this->getKomentarTable()->brKomentariUred() as $r){
$arr = $this->object_to_array($r);
print_r($arr);
}
It doesn't return number of elements and devicesID as it is written in SELECT, but return as SELECT * FROM komentar, but with no values. Is this right code or I'm making some error in my code? Other queries are OK.
Thanks in advance for your help.
In your place I would do the following steps:
replace the expression object with new \Zend\Db\Sql\Expression('COUNT(komentarID)')
I wouldn't use an alias in group by operator, it may not work. So,
replace this $select->group('ured') with
$select->group('komentar.ured')
Also, the result processing should be simplified:
$resultSet = $this->getKomentarTable()->brKomentariUred();
print_r($resultSet->toArray());