• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is it ok to delete like this $this->db->delete('freebook', array('day <' => $yesterday));

#1
[eluser]gojo[/eluser]
Is it ok to use delete like this
Code:
$yesterday = date("Y-m-d");
$query = $this->db->get_where('freebook', array('day <' => $yesterday));
        if ($query->num_rows() != 0) {
        // delete all entries where it has yesterdays date
  
  $this->db->delete('freebook', array('day <' => $yesterday));

  
        }
or should I get them in a foreach and delete one at a time
Thanks in advance Gary Smile

#2
[eluser]RobertSF[/eluser]
No, deleting rows one at a time from an SQL database is much more inefficient than deleting them all at once. What you have there seems fine, except that wouldn't your query as you have it delete all entries with dates before yesterday?

Frankly, SQL itself is hard enough that I prefer to work out the SQL, and then just do a $this->db->query('whatever I'm doing in SQL'). As far as I can tell, the CI database method query will execute any SQL statement. Smile

#3
[eluser]gojo[/eluser]
Yes its to delete everything before yesterday
Thanks for your reply

#4
[eluser]RobertSF[/eluser]
You're welcome! By the way, now that I look closer, you don't have to check first to see if there are any entries to delete. You can just do
Code:
$this->db->delete('freebook', array('day <' => date("Y-m-d")));

If you want to determine if the query deleted any rows, you can do it after the above code, like this
Code:
$deleted_count = $this->db->affected_rows();

#5
[eluser]gojo[/eluser]
Thanks that's handy to know


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2017 MyBB Group.