CodeIgniter Forums
Best way to prepare SQL statements? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Best way to prepare SQL statements? (/showthread.php?tid=4150)



Best way to prepare SQL statements? - El Forum - 11-09-2007

[eluser]europe72[/eluser]
Is there a preferred CI method?

$query = $this->db->query('SELECT name, title, email FROM my_table WHERE id = ?');


Best way to prepare SQL statements? - El Forum - 11-09-2007

[eluser]Michael Wales[/eluser]
Using Active Record:

Code:
$this->db->select('name, title, email');
$query = $this->db->getwhere('my_table', array('id'=>$id));

If on PHP5 you can use method chaining.


Best way to prepare SQL statements? - El Forum - 11-09-2007

[eluser]ELRafael[/eluser]
ooor

Code:
$this->db->select('*');
$this->db->where('lord_number', 666);
$query = $this->db->get('lords');



Best way to prepare SQL statements? - El Forum - 08-12-2008

[eluser]Sumon[/eluser]
Or this one Smile
Code:
$this->db->from('member_info');
$Condition=array('member_status'=>'approved');
$this->db->where($Condition);
return $this->db->get();
and use this in view
Code:
if(count($Recordsets->result())==0)    echo "Sorry no member exists.";
<?php foreach($SiteFeatureInfo->result() as $row): ?>
    <?=$row->site_feature_name;?>
<?php endforeach; ?>



Best way to prepare SQL statements? - El Forum - 08-13-2008

[eluser]Bramme[/eluser]
You really should put your foreach in the else part of your if... What's point of trying to loop when there's nothing to loop? Btw, chances are hot it'll throw an error then.


Best way to prepare SQL statements? - El Forum - 08-14-2008

[eluser]Sumon[/eluser]
This reduce only else(if...else) block. i.e parenthesis. However, it doesn't display error for empty recordset. Anyway, thanks.
Finally one more way of view:
Code:
if(count($Recordsets->result())==0)    echo "Sorry no member exists.";
<?php foreach($SiteFeatureInfo->result() as $row): ?>
    <?=$row->site_feature_name;?>
<?php endforeach; ?>