Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-13-2010, 06:27 AM   PM User | #1
xiaodao
Regular Coder

 
Join Date: Sep 2004
Posts: 713
Thanks: 6
Thanked 2 Times in 2 Posts
xiaodao is an unknown quantity at this point
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
xiaodao is offline   Reply With Quote
Old 01-13-2010, 07:28 AM   PM User | #2
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
Quote:
Originally Posted by xiaodao View Post
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
oesxyl is offline   Reply With Quote
Old 01-13-2010, 07:49 AM   PM User | #3
xiaodao
Regular Coder

 
Join Date: Sep 2004
Posts: 713
Thanks: 6
Thanked 2 Times in 2 Posts
xiaodao is an unknown quantity at this point
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
xiaodao is offline   Reply With Quote
Old 01-13-2010, 07:55 AM   PM User | #4
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
Quote:
Originally Posted by xiaodao View Post
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
oesxyl is offline   Reply With Quote
Old 01-13-2010, 08:07 AM   PM User | #5
xiaodao
Regular Coder

 
Join Date: Sep 2004
Posts: 713
Thanks: 6
Thanked 2 Times in 2 Posts
xiaodao is an unknown quantity at this point
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
xiaodao is offline   Reply With Quote
Old 01-13-2010, 08:14 AM   PM User | #6
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
Quote:
Originally Posted by xiaodao View Post
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
oesxyl is offline   Reply With Quote
Old 01-13-2010, 08:30 AM   PM User | #7
xiaodao
Regular Coder

 
Join Date: Sep 2004
Posts: 713
Thanks: 6
Thanked 2 Times in 2 Posts
xiaodao is an unknown quantity at this point
my question is the example, the case i provided no working is the real case
__________________
flying dagger
xiaodao is offline   Reply With Quote
Old 01-13-2010, 08:58 AM   PM User | #8
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
Quote:
Originally Posted by xiaodao View Post
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
oesxyl is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:31 AM.


Advertisement
Log in to turn off these ads.