MySQL Forums
Forum List  »  PHP

Re: Enum Toggle
Posted by: Jonathan Stephens
Date: May 16, 2005 11:28PM

Here's one way.

*Note that this uses a subquery and you must be running MySQL 4.1 or newer in order to have support for subqueries.*

Suppose you've a table created with

CREATE TABLE enums (
id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
col ENUM('yes','no') NOT NULL DEFAULT 'yes'
) ENGINE=MYISAM DEFAULT CHARSET=latin1;

Then for a given record whose id is the value of the variable $id, you can toggle the value for its col column using this:

$update_query = "UPDATE enums SET col=(SELECT CASE col WHEN 'yes' THEN 'no' ELSE 'yes' END) WHERE id='$id'";

You could perform the same logic in the PHP code instead (and you must do so if you're using MySQL 4.0 or earlier), but I think that this is a bit neater. ;)

To do that, you'd need something like this:

$update_query = "UPDATE enums SET col=" . ($enum == 'yes' ? 'no' : 'yes') . " WHERE $id='$id'";

Jon Stephens
MySQL Documentation Team @ Oracle

MySQL Dev Zone
MySQL Server Documentation
Oracle

Options: ReplyQuote


Subject
Written By
Posted
May 16, 2005 10:10AM
Re: Enum Toggle
May 16, 2005 11:28PM
May 17, 2005 08:03AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.