Enjoy an ad free experience by logging in. Not a member yet?
Register .
01-13-2010, 06:27 AM
PM User |
#1
Regular Coder
Join Date: Sep 2004
Posts: 713
Thanks: 6
Thanked 2 Times in 2 Posts
php query question
my database structure
id name status
1 abc 0
2 ggg 0
3 mimi 1
now i want to update id=2 status to 1 and the rest to 0, how can i do it in one php query?
now my steps are
update all status to 0 then update the id=2 status to 1, two steps! anyway do in one step?
__________________
flying dagger
01-13-2010, 07:28 AM
PM User |
#2
Master Coder
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
Quote:
Originally Posted by
xiaodao
my database structure
id name status
1 abc 0
2 ggg 0
3 mimi 1
now i want to update id=2 status to 1 and the rest to 0, how can i do it in one php query?
now my steps are
update all status to 0 then update the id=2 status to 1, two steps! anyway do in one step?
Code:
update tablename set status = if(id = 2, 1, 0)
best regards
01-13-2010, 07:49 AM
PM User |
#3
Regular Coder
Join Date: Sep 2004
Posts: 713
Thanks: 6
Thanked 2 Times in 2 Posts
id=2 is only one example, maybe id=1, or maybe id=2345, how to make it flexible and apply to all record?
__________________
flying dagger
01-13-2010, 07:55 AM
PM User |
#4
Master Coder
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
Quote:
Originally Posted by
xiaodao
id=2 is only one example, maybe id=1, or maybe id=2345, how to make it flexible and apply to all record?
PHP Code:
$query = "update tablename set status = if(id = $id, 2, 1)" ;
work for a single value.
best regards
01-13-2010, 08:07 AM
PM User |
#5
Regular Coder
Join Date: Sep 2004
Posts: 713
Thanks: 6
Thanked 2 Times in 2 Posts
no working...
Code:
$id=intval($id);
$userid=intval($userid);
$sql=$this->db->query("UPDATE ".$prefix."address SET default = if(addressid='$id', 2, 1) WHERE userid='$userid'") or die($this->db->error());
__________________
flying dagger
01-13-2010, 08:14 AM
PM User |
#6
Master Coder
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
Quote:
Originally Posted by
xiaodao
no working...
Code:
$id=intval($id);
$userid=intval($userid);
$sql=$this->db->query("UPDATE ".$prefix."address SET default = if(addressid='$id', 2, 1) WHERE userid='$userid'") or die($this->db->error());
not same query.
best regards
01-13-2010, 08:30 AM
PM User |
#7
Regular Coder
Join Date: Sep 2004
Posts: 713
Thanks: 6
Thanked 2 Times in 2 Posts
my question is the example, the case i provided no working is the real case
__________________
flying dagger
01-13-2010, 08:58 AM
PM User |
#8
Master Coder
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
Quote:
Originally Posted by
xiaodao
my question is the example, the case i provided no working is the real case
you changed the query and this is the reason why didn't work.
Code:
WHERE userid='$userid'"
if you want something else then what you ask, explain what you want do.
best regards
Jump To Top of Thread
Thread Tools
Rate This Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT +1. The time now is 02:31 AM .
Advertisement
Log in to turn off these ads.