Welcome Guest, Not a member yet? Register   Sign In
how do write a delete query in codeigniter
#1

[eluser]alectrash[/eluser]
I am currenlty using this code inside a model

function remove_entry($blogid)
{
$querystr = $this->db->query('DELETE * FROM tblblog WHERE dbblogid=' . $blogid);
$querydone = $this->db->query($querystr);

return TRUE;
}

but it returns

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'REMOVE * FROM `tblblog` WHERE dbblogid=1' at line 1

DELETE * FROM `tblblog` WHERE dbblogid=1


I dont know what I am doing wrong
#2

[eluser]Armchair Samurai[/eluser]
You only need to run the query once. Also, delete() returns nothing, so your second query() makes absolutely no sense - just get rid of it.

Not related to the question at hand, but you might want to look at query bindings to help properly escape your data before running a query.
#3

[eluser]Matthieu Fauveau[/eluser]
By using Active Records, you could have CI to return something by using affected_rows.

Something like that :

Code:
function remove_entry($blogid)
{
$this->db->where('dbblogid', $blogid)->delete('tblblog');
return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
}

affected_rows is actually a hack in CI because as previous poster said, MySQL doesn't return anything upons delete.
#4

[eluser]Colin Williams[/eluser]
And for goodness' sake, I always do $this->db->limit(1) to avoid something terrible happening (like completely truncating a database table on accident)!
#5

[eluser]Matthieu Fauveau[/eluser]
Colin : yeah, my bad ! Always put limit that's my motto normally Wink
#6

[eluser]alectrash[/eluser]
Hey,

Thanks alot Matt and Colin, I eventually worked it out with your help of course I know have this in my blog controller:

Code:
if($this->blog_model->remove_entry($blogid))
    {
       $this->load->view('blog/removed_blog', $data);
    }

and more importantly in the model:
Code:
function remove_entry($blogid)
    {
      $query = $this->db->where('dbblogid', $blogid);
      $query = $this->db->limit(1,0);
      $query = $this->db->delete('tblblog');
      return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }

it seems to work fine.
#7

[eluser]Matthieu Fauveau[/eluser]
Looks good except that "$query =" is useless in the function remove_entry Wink

Using Active Record you'll typically do :

Code:
$this->db->select('field1, field2, field3');
$query = $this->db->getwhere('table', array('field4 =' => 'something'), 1);

Here $query is actually useful as we should receive data.
#8

[eluser]JoostV[/eluser]
It's also a good idea to do some data checking on your ID before deleting. Following code only deletes a record for which the ID is an integer and greater than zero.
Code:
function remove_entry($blogid)
{
    if (is_int($blogid) && $blogid > 0) {
        $this->db->where('dbblogid', $blogid);
        $this->db->limit(1,0);
        $this->db->delete('tblblog');
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }
    else {
        return FALSE;
    }
}




Theme © iAndrew 2016 - Forum software by © MyBB