public function insrt_into_aprlog($id)
{
$query = 'insert into log_indent_apval (select id,indent_req_id,item_id,req_qty,approval_qty,approval_status,prod_categry,prod_type from tra_inent_rq_itm_dt where id ='.$id.')';
$result = $this->db->query($query);
$resultarr = $result->result_array();
}
my controller line:
$this->outpatient_model->insrt_into_aprlog($indent_id);
error : Call to a member function result_array() on a non-object
even i tried using in codeigniter model insert_batch(); still no use.
result_array() is used to generate query result . You are trying to insert data in your table i guess, so you shouldn't use that . Try below code.
public function insrt_into_aprlog($id)
{
$query = 'insert into log_indent_apval (select id,indent_req_id,item_id,req_qty,approval_qty,approval_status,prod_categry,prod_type from tra_inent_rq_itm_dt where id ='.$id.')';
return $this->db->query($query);
}
Please visit this
Reference
Look into your query.
INSERT INTO table1 ( column1, column2, someInt, someVarChar )
SELECT table2.column1, table2.column2, 8, 'some string etc.'
FROM table2
WHERE table2.ID = 7;
$query is being initialized to a string and then is accessed for an object. This is inconsistent for sure.
$query is a string, I guess you should use $result->result_array().
Related
In order to write query in codeigniter you need to write something like $this->db->query or $someDB->query. But what if I want to join tables from two different databases?
I know that I can do it through pure php, using mysqli_connect and writing something like:
SELECT * FROM db1.table1 JOIN db2.table2
But is there a way to do it using codeigniter?
Use query method from CodeIgniter and pass any complex sql query trough:
$query = $this->db->query("SELECT * FROM dbname1.table t1 JOIN db2.table t2 ON t2.column = t1.column");
foreach ($query->result() as $row)
{
print_r($row);
}
This simple join of student and teacher table with same column name , Hope you will get it .
$this->db->select("s.*,t.*");
$this->db->from("student as s");
$this->db->join("teacher as t","s.student_id = t.student_id","both");
$result = $this->db->get()->result_array();
return $result;
also you want single row try row_array(); instead of result_array();
You can try to use the function I am using in a project I am working on right now... Please see the code below...
function join_table()
{
$this->db->select(//column name, //column name, //column name);
$this->db->from(//table1 name);
$this->db->join(//table2 name, //table1 name.//column name = //table2 name.//column name');
$this->db->where(//condition);
return $this->db->get()->result();
}
I want to write the given query in codeigniter , how can I do so?
SELECT * FROM `user` WHERE `id` IN (select `provider_id` from `services`);
I gone through codeigniter's where_in() but i'm getting wrong result , here what I have done,
$this->db->select('*');
$this->db->from(USER);
$this->db->where_in('id', 'select `provider_id` from `services`');
$query = $this->db->get();
return $query->num_rows();
it is returning 0, while the query above, when run directly in phpmyadmin, returns 1 record.
You can use this simple way $this->db->query('your query');
Please check this post, hope it will help you Using Mysql WHERE IN clause in codeigniter
You can pass direct queries in where class by passing second parameter as NULL and third parameter as FALSE
<?php
$where = 'id IN (select `provider_id` from `services`)';
$this->db->select('*');
$this->db->from(USER);
$this->db->where($where, NULL, FALSE);
$query = $this->db->get();
return $query->num_rows();
Use $this->db->query();
$query=$this->db->query("SELECT * FROM `user` WHERE `id` IN (select `provider_id` from `services`)");
$num=$query->num_rows();
$result= $query->result();
This code is working perfectaly in mysql run command
SELECT employeeCode
FROM employee_details
WHERE employeeCode
IN (
SELECT DISTINCT (employeeCode) FROM quiz_answer_detailsWHERE submitTime
IN (SELECT MIN( submitTime ) FROM quiz_answer_details WHERE quizId
IN (SELECT id FROM quiz_details WHERE uploadtime = '2014-04-03')
AND answer IN (SELECT answer FROM quiz_details WHERE uploadtime = '2014-04-03'))
)
But I want to use this code on my codeigniter, but it is not working.
My codeigniter query code is
$this->db->select('employeeCode');
$this->db->from('employee_details');
$this->db->where_in('employeeCode');
$this->db->select('DISTINCT(employeeCode)');
$this->db->from('quiz_answer_details');
$this->db->where_in('submitTime');
$this->db->select('min(submitTime)');
$this->db->from('quiz_answer_details');
$this->db->where_in('quizId');
$this->db->select('id');
$this->db->from('quiz_details');
$this->db->where('uploadtime',"2014-04-03");
$this->db->where_in('answer');
$this->db->select('answer');
$this->db->from('quiz_details');
$this->db->where('uploadtime',"2014-04-03");
$query=$this->db->get();
print_r($query);
if($query->num_rows>=1)
{
return $query;
}
else
{
return false;
}
What is wrong please help me
The problem lies with this code and subsequent similar uses of where_in
$this->db->where_in('employeeCode');
You have given the where parameter value but not what to match with.
for eg.
$this->db->where_in('employeeCode',$subQuery1);
The documentation of where_in:
$this->db->where_in();
Generates a WHERE field IN ('item', 'item') SQL query joined with AND
if appropriate
$names = array('Frank', 'Todd', 'James');
$this->db->where_in('username', $names); // Produces: WHERE username
IN ('Frank', 'Todd', 'James')
You have to create a separate sub query for each invocation of where_in.
You should re write you subquery and use joins instead to get the better performance,without having full information regarding your tables/relationship and desired result i can't provide you the new query but you can use your subquery in active record's where function
$subquery=" SELECT DISTINCT (employeeCode) FROM quiz_answer_detailsWHERE submitTime
IN (SELECT MIN( submitTime ) FROM quiz_answer_details WHERE quizId
IN (SELECT id FROM quiz_details WHERE uploadtime = '2014-04-03')
AND answer IN (SELECT answer FROM quiz_details WHERE uploadtime = '2014-04-03')) ";
$this->db->select('employeeCode');
$this->db->from('employee_details');
$this->db->where('employeeCode IN('.$subquery.')',null,FALSE);
$query=$this->db->get();
You should pass third parameter as FASLE in order to prevent the query to be quoted by bacticks
Or you can use query() fucntion to run your raw queries
$query=$this->db->query(' your full query here');
$query->result();
Probably a bit of a newbie question, but I'm self-taught, and I'm trying to edit some code that is out of my comfort zone, and coming unstuck. Please help!
It is a function that selects data from a MySQL database and returns an array. It uses a couple of other 'core' functions to help with the DB connections.
The original script looks like this:
class Core{
protected $db, $result;
private $rows;
public function __construct() {
$this->db = new mysqli('localhost', 'root', 'password', 'db');
}
public function query($sql){
$this->result = $this->db->query($sql);
}
public function rows(){
for($x = 1; $x <= $this->db->affected_rows; $x++){
$this->rows[] = $this->result->fetch_assoc();
}
return $this->rows;
}
}
class Chat extends Core{
public function fetchMessages(){
$this->query("
SELECT `chat`.`message`,
`users`.`username`,
`users`.`user_id`
FROM `chat`
JOIN `users`
ON `chat`.`user_id` = `users`.`user_id`
ORDER BY `chat`.`timestamp`
DESC
");
return $this->rows();
}
}
The problem is, within the fetchMessages() function in the Chat class there is a SELECT clause which JOINS the "chat" table with the "users" table to fetch the username. If, for whatever the reason, (deleted, banned, quit, etc), the user ID doesn't exist in the user table, the SELECT clause returns no results.
In order for it still to return the message if the user doesn't exist, I think I need to seperate the JOIN into 2 SELECT CLAUSES:
First, SELECT message, user_id FROM chat ORDER BY timestamp DESC;
Then, SELECT username FROM users WHERE user_id = $user_id and RETURN "Guest" if no rows are found.
(I have got the pseudo-code or logic right in my head, I just can't code it!)
My problem is, because I am using the $this-> notation, I don't know how to include a second instance inside the function. What I want to do is something like this:
public function fetchMessages(){
$this->query("
SELECT `message`, `user_id` FROM `chat` ORDER BY `chat`.`timestamp` DESC
");
$rows = $this->rows();
foreach ($rows as $row) {
$uid = $row['user_id'];
$this[2]->query("
SELECT `username` FROM `users` WHERE `user_id` = `$uid`;
");
$user = $this[2]->rows();
if ( $user['username'] == "" ) {
$username = "Guest";
} else {
$username = $user['username'];
}
$return_array[] = array($row['message'],$username);
}
return $return_array;
}
Can anyone understand what I am trying to do, and re-write my pseudo-code so that it doesn't use two '$this->'s and actually works?
I would really appreciate any help...
You simply need to change JOIN users to LEFT JOIN users.
Your current code performs an INNER JOIN, which produces result rows only when there are rows to join on both tables. A LEFT JOIN will produce results for all rows that exist on the left-hand-side table, substituting NULL for the values from the right-hand-side table when no corresponding rows exist there.
In your case, this means that you will be getting back rows for all messages, even if there is no corresponding user for some of them.
See also a visual explanation of SQL joins.
I'm trying to retrieve a count of all unique values in a field.
Example SQL:
SELECT count(distinct accessid) FROM (`accesslog`) WHERE record = '123'
How can I do this kind of query inside of CodeIgniter?
I know I can use $this->db->query(), and write my own SQL query, but I have other requirements that I want to use $this->db->where() for. If I use ->query() though I have to write the whole query myself.
$record = '123';
$this->db->distinct();
$this->db->select('accessid');
$this->db->where('record', $record);
$query = $this->db->get('accesslog');
then
$query->num_rows();
should go a long way towards it.
try it out with the following code
function fun1()
{
$this->db->select('count(DISTINCT(accessid))');
$this->db->from('accesslog');
$this->db->where('record =','123');
$query=$this->db->get();
return $query->num_rows();
}
You can also run ->select('DISTINCT `field`', FALSE) and the second parameter tells CI not to escape the first argument.
With the second parameter as false, the output would be SELECT DISTINCT `field` instead of without the second parameter, SELECT `DISTINCT` `field`
Since the count is the intended final value, in your query pass
$this->db->distinct();
$this->db->select('accessid');
$this->db->where('record', $record);
$query = $this->db->get()->result_array();
return count($query);
The count the retuned value
Simple but usefull way:
$query = $this->db->distinct()->select('order_id')->get_where('tbl_order_details', array('seller_id' => $seller_id));
return $query;