Example of a Multi Condition Delete with Zend framework
Asked Answered
I

3

6

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);
Indoaryan answered 22/5, 2009 at 20:38 Comment(0)
J
8

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)

Jostle answered 22/5, 2009 at 20:45 Comment(2)
Will this come out as: "DELETE FROM messages WHERE message_id = 1 AND user_id = 2" or "DELETE FROM messages WHERE (message_id = 1) AND (user_id = 2) ?Indoaryan
If I read the source correctly, it'll actually put the parentheses around each... /library/Zend/Db/Adapter/Abstract.php, _whereExpr (line 564 or so...)Jostle
S
29

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.

Sceptic answered 31/7, 2010 at 12:16 Comment(0)
J
8

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)

Jostle answered 22/5, 2009 at 20:45 Comment(2)
Will this come out as: "DELETE FROM messages WHERE message_id = 1 AND user_id = 2" or "DELETE FROM messages WHERE (message_id = 1) AND (user_id = 2) ?Indoaryan
If I read the source correctly, it'll actually put the parentheses around each... /library/Zend/Db/Adapter/Abstract.php, _whereExpr (line 564 or so...)Jostle
B
-3

Use this , it is working...

$data = array(
    'bannerimage'=>$bannerimage
);

$where = $table->getAdapter()->quoteInto('id = ?', 5);

$table->update($data, $where);
Batt answered 16/12, 2011 at 5:48 Comment(1)
Especially when the answer shows an "update" instead of a "delete".Phrasing

© 2022 - 2024 — McMap. All rights reserved.