CodeIgniter Forums
Select by active record... help - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Select by active record... help (/showthread.php?tid=38557)



Select by active record... help - El Forum - 02-11-2011

[eluser]Unknown[/eluser]
Hello!

I'm sorry for my English

I try to select from database (mysql) by ActiveRecord

Code:
$this->db->select('results.id_url, results.checktime, results.result, results.responsetime, results.responselength, results.responsespeed, urls.url')
->from('results,url_urls')
->where(array("url_results.id_url" =>$urlId,"url_urls.id_url"=>"url_results.id_url"))
->get();
echo $this->db->last_query();

sql code of this query:

SELECT `url_results`.`id_url`, `url_results`.`checktime`, `url_results`.`result`, `url_results`.`responsetime`, `url_results`.`responselength`, `url_results`.`responsespeed`, `url_urls`.`url` FROM (`url_results`, `url_urls`) WHERE `url_results`.`id_url` = '1' AND `url_urls`.`id_url` = 'url_results.id_url'

please, Help me to create righty query Smile I don't know how make condition part

If I write '...->where("url_results.id_url =".$urlId." and url_urls.id_url = url_results.id_url")' to query return data, but this syntax does not suit me...

UPD: I'm using tableprefix "url_"


Select by active record... help - El Forum - 02-13-2011

[eluser]sikko[/eluser]
Well, this is how I would do it:


Code:
$this->db->select('ur.id_url, ur.checktime, ur.result, ur.responsetime, ur.responselength, ur.responsespeed, uu.url');
$this->db->from('url_results ur, url_urls uu');
$this->db->where('ur.id_url', $urlId);
$this->db->where('uu.id_url', 'ur.id_url', false);

Please note the false in the last where clause. It's used for the field name not to be escaped.

Try it and let us know.


Select by active record... help - El Forum - 02-13-2011

[eluser]Unknown[/eluser]
Thank you!

Code:
$this->db->select('a.id_url, a.checktime, a.result, a.responsetime, a.responselength, a.responsespeed, b.url')
->from('results as a,url_urls as b')
->where(array("a.id_url" =>$urlId,"b.id_url"=>"a.id_url"),null,false)
->get();

excellent!