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);

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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)

2 Comments

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) ?
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...)
-3

Use this , it is working...

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

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

$table->update($data, $where);

1 Comment

Especially when the answer shows an "update" instead of a "delete".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.