CodeIgniter Forums
what is the results from $this->db->delete() - 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: what is the results from $this->db->delete() (/showthread.php?tid=25054)



what is the results from $this->db->delete() - El Forum - 11-29-2009

[eluser]dcbartlett[/eluser]
Hellow,

I'm trying to find what the default results from $this->db->delete() are.

$this->db->delete() returns bool(true), however, i do now know what else it will return.

i'm trying to achieve the ability to tell a user that a record does not exist when they try to delete it.

if ($this->db->delete($table)) {
return 1;
} else {
return 0;
}

i have another function that looks for 1 and says record deleted, if it gets 0, it tells the user that record can not be deleted.

Any help is appreciated.


what is the results from $this->db->delete() - El Forum - 11-29-2009

[eluser]jedd[/eluser]
Hi dcbartlett and welcome to the CI forums.

[quote author="dcbartlett" date="1259557146"]
I'm trying to find what the default results from $this->db->delete() are.
[/quote]

One of the handy features of CI is that, by definition, you have access to the source.

Reading through system/database/DB_active_rec.php is somewhat instructive here, once you get down to the delete() function.

The doc block isn't hugely helpful, as it just tells us the the return type is an object. There are five return lines in that function, variously returning boolean, the output of display_error(), nothing, or this->query($sql).

So .. take your pick Smile

Quote:i'm trying to achieve the ability to tell a user that a record does not exist when they try to delete it.

I'd suggest you may need to write a check function first to confirm the element exists, and possibly another afterwards to confirm the element doesn't.


what is the results from $this->db->delete() - El Forum - 11-29-2009

[eluser]dcbartlett[/eluser]
thanks for the info about the returned values, i found that out the hard way, and yes indeed i did have to write a check for it due to the fact that a object tends to love to return bool(true)...lol


what is the results from $this->db->delete() - El Forum - 11-29-2009

[eluser]dcbartlett[/eluser]
if anyone has a better solution than:

function delete_record($table, $id)
{

$d = $this->db->get_where($table, array('id' => $id));
if ($d->result()) {
$this->db->where('id',$id);
$this->db->delete($table);
return 1;
} else {
return 0;
}
}

Please let me know


what is the results from $this->db->delete() - El Forum - 11-30-2009

[eluser]crnalajna[/eluser]
Code:
function delete($id){
        $this->db->delete($this->table_name, array('id' => $id));
        if ($this->db->affected_rows() > 0)
            return TRUE;
        return FALSE;
    }



what is the results from $this->db->delete() - El Forum - 11-30-2009

[eluser]dcbartlett[/eluser]
Much cleaner and efficient, thanks.


what is the results from $this->db->delete() - El Forum - 11-30-2009

[eluser]jedd[/eluser]
bOkI - excellent call, thanks.

I've updated the [url="/wiki/SQL"]SQL FAQ[/url] in the wiki with some info about this.

dcbartlett - you may want to have a read, as there's some caveats in the CI manual, the CI source, and the PHP.net page about this functionality.


what is the results from $this->db->delete() - El Forum - 11-30-2009

[eluser]dcbartlett[/eluser]
excellent, thatnks for the updates on the sql faq.