[SOLVED]Update 2 fields in the same query

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

[SOLVED]Update 2 fields in the same query

Post by vigge89 »

How do I update 2 or more fields in one query?
i've tested this, but i can't get it to work;

Code: Select all

<?php
$sql = "UPDATE skin, email SET skin = '$skin' SET email = '$email' WHERE nick = '$usrname'";
@mysql_query($sql);
?>
Im a beginner at MySQL and i have no idea if it's possible at all
Last edited by vigge89 on Thu Dec 25, 2003 11:45 am, edited 1 time in total.
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

Code: Select all

$sql = "UPDATE the_table SET name = 'nay', obbession = 'php', the_other_obbession = 'heredoc' WHERE nick = '$username'";
$result = mysql_query($sql, $connection);
Hope that helps vigge ;)

-Nay
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

MySQL manual wrote: 6.4.5 UPDATE Syntax
UPDATE [ LOW_PRIORITY ] [ IGNORE ] tbl_name
SET col_name1=expr1 [ , col_name2=expr2, ... ]
[ WHERE where_definition ]
[ LIMIT # ]
You can update several fields in one query:

Code: Select all

update sometable set somefield='this', someotherfield='that';
but you can't reference more then one table:

Code: Select all

update sometable, someothertable.... // this won't work
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

thanks!
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

BTW, if i want to store text in a mySQL field, which fieldtype should i use?
It would be used as notes, ie; a notepad, so it should be able to contain long texts, but not in the megabyte-size.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

varchar and char are limited to 128 characters so text would be the best in this case.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

ok, but what should i set the lenght to? null?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

you cant specify the lenght of text column, so just text:

Code: Select all

create tables sometable (somecolumn text, someothercolumn someothertype,....);
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

im doing it in PHPmyadmin, so i thought i needed to fill in all fields, but ok, i'll just leave it then, thanks!
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Weirdan wrote:varchar and char are limited to 128 characters so text would be the best in this case.
Actually, char is limited to 0-255 and varchar to 1-255.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

JAM wrote:
Weirdan wrote:varchar and char are limited to 128 characters so text would be the best in this case.
Actually, char is limited to 0-255 and varchar to 1-255.
Mine are limited to 128 ;P (MySQL tuned by me)
Post Reply