Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Example of a Multi Condition Delete with Zend framework

Can someone give me an example of how I would delete a row in mysql with Zend framework when I have two conditions?

i.e: (trying to do this)

"DELETE FROM messages WHERE message_id = 1 AND user_id = 2"

My code (that is failing miserably looks like this)

// is this our message?
$condition = array(
                   'message_id = ' => $messageId,
                   'profile_id = ' => $userId
);

$n = $db->delete('messages', $condition);
like image 684
MichaelICE Avatar asked May 22 '09 20:05

MichaelICE


2 Answers

Better to use this:

$condition = array(
    'message_id = ?' => $messageId,
    'profile_id = ?' => $userId
);

The placeholder symbols (?) get substituted with the values, escapes special characters, and applies quotes around it.

like image 127
robbie Avatar answered Oct 20 '22 00:10

robbie


Instead of an associative array, you should just be passing in an array of criteria expressions, ala:

$condition = array(
    'message_id = ' . $messageId,
    'profile_id = ' . $userId
);

(and make sure you escape those values appropriately if they're coming from user input)

like image 23
great_llama Avatar answered Oct 20 '22 01:10

great_llama