Welcome Guest, Not a member yet? Register   Sign In
Best way to prepare SQL statements?
#1

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

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

[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.
#3

[eluser]ELRafael[/eluser]
ooor

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

[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; ?>
#5

[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.
#6

[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; ?>




Theme © iAndrew 2016 - Forum software by © MyBB