Array to string conversion while passing query data(array) in where clause. I tried enough so now i'm going to scared of. I tried to to replace ->result() with ->row() in $ord = "query" but no sake. I Please any body help me, what should i do now. Thank you. Here is my code.
public function order_join()
{
$ord = $this->db->query('SELECT `Order_ID` FROM `order` WHERE `DateTime` = (SELECT MAX(DateTime) FROM `order`)')->result();
$query = "SELECT a.Order_ID, a.User_ID,a.Pro_ID, a.Price as Total_Amount, SUM(b.Amount)
as Total_Amount_Recieved, (a.Price - SUM(b.Amount)) AS Remaining_Amount
FROM `order` a JOIN `payments` b ON a.Order_ID = b.Order_ID WHERE a.Order_ID = '".$ord."' ";
return $this->db->query($query)->row();
}
and this is error.
Related
I have given my Codeigniter code below, here i need to update a record using join conditions. I used the below code, But shows error
$condition="a.assignto='0' and a.recstatus='1' and b.location='$location' and '$category' IN(SELECT categoryid FROM `tq_productcategory` where productid=c.productid and recstatus='1')";
$this->db->set($data);
$this->db->where($condition);
$this->db->limit($limit);
$this->db->join('tq_customer b','a.customerid=b.customerid');
$this->db->join('tq_product c','a.productid=c.productid');
$this->db->order_by("a.created_on", "asc");
$this->db->update('tq_customerservicesupport a');
Below is my error Msg
Unknown column 'b.location' in 'where clause'
UPDATE `tq_customerservicesupport` `a` SET `assignto` = '10' WHERE `a`.`assignto` = '0' and `a`.`recstatus` = '1' and `b`.`location` = '3227' and '1' IN(SELECT categoryid FROM `tq_productcategory` where productid = `c`.`productid` and `recstatus` = '1') ORDER BY `a`.`created_on` ASC LIMIT 1
Filename: D:/wamp/www/tooquik/system/database/DB_driver.php
Try this:
$sql = "UPDATE tq_customerservicesupport AS a JOIN tq_customer AS b ON a.customerid = b.customerid JOIN tq_product AS c ON a.productid = c.productid SET $data WHERE $condition ORDER BY a.created_on ASC LIMIT $limit";
$this->db->query($sql);
Make sure that the query is correct cause i just wrote it based on your data and better to check it in phpmyadmin to make sure it works fine with no errors.
Im working on ecommerce platform. I have a query in normal form. i want to convert to codeigniter.
this is my query
SELECT products.product_name,products.product_id,products.short_description,pi.img,
CASE WHEN products.sp_price=0 THEN products.price WHEN products.sp_price!=0 THEN products.sp_price END as pprice FROM
(`offers_products`) JOIN `products` ON `offers_products`.`product_id` = `products`.`product_id`
LEFT JOIN (SELECT image_name as img,product_id as pid from product_images GROUP BY pid)pi
ON `products`.`product_id` = `pi`.`pid` .
How do i convert this to codeigniter query.
I tried, but getting syntax error. Please help me, im new to codeigniter.
There is no need to convert your query. And also the is no rule that you should use codeigniter query only.
you can use
$res = $this->db->query("your query here")->result();
$res will have that result() you want.
This will help you.
For more reference, check here
Just use
public function method_name()
{
$query = $this->db->query("SELECT products.product_name,products.product_id,products.short_description,pi.img, CASE WHEN products.sp_price=0 THEN products.price WHEN products.sp_price!=0 THEN products.sp_price END as pprice FROM (`offers_products`) JOIN `products` ON `offers_products`.`product_id` = `products`.`product_id` LEFT JOIN (SELECT image_name as img,product_id as pid from product_images GROUP BY pid)pi ON `products`.`product_id` = `pi`.`pid`");
$result = $query->result_array();
return $result;
}
We use result_array for pass data as Objective array
I'm converting my existing website to CI, and I have been trying for several days to convert this query to CI-friendly code:
$result = mysql_query("
SELECT t1.mnumber, t1.mcontent, t1.mcontact
FROM sms t1
JOIN (
SELECT mContent,mcontact, mnumber, MAX(mID) mID
FROM sms
GROUP BY mContact
) t2 ON t1.mcontact = t2.mcontact AND t1.mid = t2.mid
GROUP BY t1.mContact
ORDER BY t1.mid DESC
");
But no matter what I try, I can't get the correct result on CI.
I hope you guys can help me out here!
The closest to a result that i did get, was when i used the subquery hack.
However, out of frustration i deleted the block of code and kept trying.
I decided to use a flat query, like the one posted above. This almost gives me results.
$query = $this->db->query("SELECT t1.mnumber, t1.mcontent, t1.mcontact FROM sms t1
JOIN (SELECT mContent,mcontact, mnumber, MAX(mID) mID FROM sms GROUP BY mContact) t2
ON t1.mcontact = t2.mcontact AND t1.mid = t2.mid GROUP BY t1.mContact ORDER BY t1.mid DESC");
$contacts = array();
//Add data to our array
foreach($query->result() as $row){
echo $row->mNumber;
}
return $contacts;
However, in my view i get the notice "Message: Undefined property: stdClass::$mNumber"
So still no results, plus i prefer the CI query method.
You can use it in codeigniter like this
$query = " SELECT t1.mnumber, t1.mcontent, t1.mcontact
FROM sms t1
JOIN (
SELECT mContent,mcontact, mnumber, MAX(mID) mID
FROM sms
GROUP BY mContact
) t2 ON t1.mcontact = t2.mcontact AND t1.mid = t2.mid
GROUP BY t1.mContact
ORDER BY t1.mid DESC";
$result = $this->db->query($query);
return $result->result();
2nd Method
You can use sub query way of codeigniter to do this for this purpose you
will have to hack codeigniter. like this. Go to system/database/DB_active_rec.php
Remove public or protected keyword from these functions
public function _compile_select($select_override = FALSE)
public function _reset_select()
Now subquery writing in available And now here is your query with active record
$select = array(
'mContent',
'mcontact',
'mnumber',
'MAX(mID) mID'
);
$this->db->select($select);
$this->db->from('sms');
$this->db->group_by('mContact');
$subquery = $this->db->_compile_select(); // get the query string
$this->db->_reset_select(); // reset so it can newly form the query
unset($select);
$select = array(
't1.mnumber',
't1.mcontent',
't1.mcontact'
);
$this->db->select($select);
$this->db->join('',"($subquery)");
$this->db->from('sms t1');
$this->db->group_by('t1.mContact');
$this->db->order_by('t1.mid','DESC');
$result = $this->db->get();
return $result->result();
And the thing is done. Cheers!!!
Note : While using sub queries you must use
$this->db->from('myTable')
instead of
$this->db->get('myTable')
which runs the query.
Er, shouldn't that be (at a guess)...
SELECT t1.mnumber
, t1.mcontent
, t1.mcontact
, t1.mid
FROM sms t1
JOIN
( SELECT mcontact
, MAX(mID) mID
FROM sms
GROUP BY mContact
) t2
ON t1.mcontact = t2.mcontact
AND t1.mid = t2.mid
ORDER
BY t1.mid DESC;
This may not fully answer your question, but for those of you who are Googling for a "JOIN AND" (so that you can AND some extra stuff on the Join, before the WHERE clause kicks in), here's a hacky way to do it:
$this->db->join('table_to_join', 'first_table.connect_id = table_to_join.id AND table_to_join.some_filter != 0');
Definitely not pretty, but that obviously forces the AND to occur immediately after the JOIN, before you get into the WHERE stuff for the rest of your query on first_table
Well, its easy to implement in CI version 3+
$subQuery = $this->db->select( 'table2.*' )
->join( 'table3', 'table3.orderId = table2.fkOrderId', 'INNER' )
->where()->get_compiled_select( 'table2' );
$this->db->select( 'table1.*' )
->join( '(' . $subQuery . ') AS b', 'b.`fkCouponId` = table1.couponId', 'LEFT', FALSE )
->get( 'table1' )
TIP: Make sure you don't use any query builder methods above the subquery part.
Hi I want to convert my normal mysql query to zend.db.select;
I want to use this script:
$select = $db->select();
// Add a FROM clause
$select->from( ...specify table and columns... )
// Add a WHERE clause
$select->where( ...specify search criteria... )
// Add an ORDER BY clause
$select->order( ...specify sorting criteria... );
$select->limit(20, 10);
for my query below
SELECT
IF(derived_messages.toid = '$user', derived_messages.fromid,
derived_messages.toid) friend1,c.UserName,
derived_messages.message, derived_messages.fromid, derived_messages.toid,
derived_messages.is_read,derived_messages.type,derived_messages.id as mesid,
derived_messages.date,
(SELECT M.message_id FROM messagesmapped M where M.message_id= derived_messages.id AND M.user_id ='$user' AND M.important = 1) as MesMapid
FROM
(
SELECT *
FROM messages
WHERE messages.deleted_by NOT
IN ( $user )
ORDER BY Date DESC
) derived_messages
INNER JOIN Users c ON c.MemberID = IF(derived_messages.toid = '$user', derived_messages.fromid,
derived_messages.toid)
WHERE (derived_messages.id IN
(SELECT M.message_id FROM messagesmapped M where M.message_id= derived_messages.id AND M.user_id ='$user' AND M.important = 1)
AND
(derived_messages.toid='$user' OR derived_messages.fromid='$user'))
GROUP BY friend1 ASC
ORDER BY derived_messages.date DESC, derived_messages.id DESC LIMIT $limit $offset
I hope someone can help m on this.
Thank you.
It's possible but unlikely someone will write the query for you.
My recommendation on tackling such a query is to write each individual subquery as its own Zend_Db_Select object and then build the final query using the subqueries that you already have objects for.
Zend_Db_Select doesn't directly support the IF function, so for that you will need to use Zend_Db_Expr to add that statement into your select.
Here is a basic example of what I am talking about. Let's build the following query:
SELECT IF(msg.toId = 'drew010', msg.fromId, msg.toId), id, name, age, history.ip
FROM users
JOIN history ON users.id = history.userId
WHERE users.id = (
SELECT id FROM users WHERE loginCount > 1000
)
GROUP BY id,
ORDER BY age DESC
First build the subselect that select users where loginCount > 1000.
$subquery1 = $db->select()
->from('users', array('id'))
->where('loginCount > ?', 1000);
Next, build the outer query with the IF function:
$cols = array(
new Zend_Db_Expr('IF(' . $db->quoteInto('msg.toId = ?', 'drew010') . '), msg.fromId, msg.toId'),
'id', 'name', 'age'
);
$query = $db->select()
->from('users', $cols)
->join('history', 'users.id = history.userId', array('ip'))
->where('id = ?', $subquery1)
->group('id')
->order('age DESC');
echo $query;
The output:
SELECT
IF(msg.toId = 'drew010', msg.fromId, msg.toId),
`users`.`id`,
`users`.`name`,
`users`.`age`,
`history`.`ip`
FROM `users`
INNER JOIN `history`
ON users.id = history.userId
WHERE id = (
(SELECT `users`.`id`
FROM `users`
WHERE (loginCount > 1000))
)
GROUP BY `id`
ORDER BY `age` DESC
So the way to go is break the entire query into individual queries first, and then construct the outer query. Just have patience and take it slow. That and read over the Zend_Db_Select docs to get a full picture of what you have available to you.
I have googled my problem but didnt get the answer.
I want to list all of the results of below sql including NULL (when COUNT(review.id) return 0 also) but instead i just got the results of articles of place that only contains review.
$sql = "SELECT tbl_place.id, tbl_place.region_id, tbl_place.subregion_id, tbl_place.title, tbl_place.metalink, tbl_place.img_thumbnail, tbl_place.summary, tbl_place.category1_id, tbl_place.category2_id, tbl_place.category3_id, COUNT(review.id) AS total_review FROM tbl_place
JOIN review ON tbl_place.id = review.place_id
WHERE
tbl_place.category1_id = '32' AND
tbl_place.status = '1' AND
review.rating != '0.00'
GROUP BY tbl_place.id
ORDER BY total_review $by
LIMIT $limit OFFSET $offset";
please use left join for review table instead of join. join is by default inner join so it will take only matched records.
the sql should be :
$sql = "SELECT tbl_place.id,
tbl_place.region_id,
tbl_place.subregion_id,
tbl_place.title,
tbl_place.metalink,
tbl_place.img_thumbnail,
tbl_place.summary,
tbl_place.category1_id,
tbl_place.category2_id,
tbl_place.category3_id,
(SELECT COUNT(*) FROM review WHERE review.rating != '0.00' AND tbl_place.id = review.place_id ) AS total_review
FROM tbl_place WHERE
tbl_place.category1_id = '32' AND
tbl_place.status = '1'
GROUP BY tbl_place.id
ORDER BY total_review $by";
it's working! thx guys!