Welcome Guest, Not a member yet? Register   Sign In
How to compare two columns in a JOIN with CI
#1

[eluser]tochie[/eluser]
hello brethren,

I want to do something like this in CI
Code:
JOIN messages_sent ON messages_sent.msg_key=$msg_key AND messages_sent.msg_from=$mem_code

I tried doing it this way but it didnt work correctly..

Code:
$this->db->join('messages_sent', 'messages_sent.msg_key='.$msg_key);
$this->db->join('messages_sent', 'messages_sent.msg_from='.$mem_code);

Please any suggests as to the correct way to compare two columns in one query...Thanks
#2

[eluser]cahva[/eluser]
Join is used to join 2 related tables together. I think you are trying to do a JOIN when you should just use WHERE condition. If you are indeed trying to join 2 tables, what does $msg_key and $mem_code contain?
#3

[eluser]zyzzzz[/eluser]
I believe ActiveRecord just makes things more difficult, I've stopped using it in my code.
Use something like this:
Code:
$sql = 'SELECT * FROM table WHERE something = ? AND somethingelse = ?';
$bindings = array($value_1, $value2);
$results = $this->db->query($sql, $bindings)->result_array();

You know exactly what the code is doing and using that bindings array will automagically make everything safe.




Theme © iAndrew 2016 - Forum software by © MyBB