![]() |
![]() |
![]() |
||||
Welcome to the GoFuckYourself.com - Adult Webmaster Forum forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact us. |
![]() ![]() |
|
Discuss what's fucking going on, and which programs are best and worst. One-time "program" announcements from "established" webmasters are allowed. |
|
Thread Tools |
![]() |
#1 |
Confirmed User
Join Date: Feb 2004
Location: Alberta, Canada
Posts: 4,707
|
Need some simple mysql help
Alright
I have some chatbots, and id like to save conversations for each user that my bots talk to What would be the best way of doing this? My table has 2 columns, 'user' and 'chattext'. For every message received, what would be the best way of updaitng the table? Should i go <Select chattext from myTable where user = 'some_user'>.. If theres no results, then add the user.. if there is, then do <update mytable set chattext = mysql_result(chattext) & 'new text' where user='some_user'>?? it seems wrong cause im searching for user='some_user' two times.. once to retrieve chattext (if there is any - i do this to 1) check if the user exists in the table and 2)to store chattext for later when im updating the table). and the other to update the table where user='some_user'. Can this be done in a better way? heres what im thinking - pseudo mysql code "update mytable append(new_chat_text) to chattext where user='some_user'.... on error insert into table mytable(user,chattext) values some_user, new_chat_text" but i dont know enough sql to do this right any suggestions?
__________________
ICQ: 298-523-037 |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#2 |
Confirmed User
Join Date: Feb 2004
Location: Alberta, Canada
Posts: 4,707
|
come on boys this is elementary
__________________
ICQ: 298-523-037 |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#3 |
Confirmed User
Join Date: May 2004
Location: SW Palm Bay, Florida
Posts: 1,323
|
So you want 1 unique row for each user and you are going to use the chattext column to store the last message the user typed ?
__________________
subarus. |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#4 |
Confirmed User
Join Date: May 2004
Location: SW Palm Bay, Florida
Posts: 1,323
|
This question really doesn't make much sense. You are asking about updating the chattext column, but your codes looks like you are trying to append to it...
__________________
subarus. |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#5 |
Confirmed User
Join Date: Feb 2004
Location: Alberta, Canada
Posts: 4,707
|
im trying to append it
![]() but i dont know much about mysql, so i thought i need the update command..
__________________
ICQ: 298-523-037 |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#6 | |
Confirmed User
Join Date: May 2004
Location: SW Palm Bay, Florida
Posts: 1,323
|
Quote:
Code:
$username = $_COOKIE['username']; //What user are we updating ? $chattext = $_POST['chattext']; //What text did this user submit ? //Let's check to see if this username is in the table. $usernamequery = "SELECT username FROM members WHERE username='$username'; $username = @mysql_query($usernamequery); $numusername = mysql_num_rows($username);//Does this username exist ? if ($numusername = 0) {//If username doesn't exist add to table. $adduserquery = "INSERT INTO members (username,chattext) VALUES ('$username', '$chattext'); } elseif ($numusername = 1) {//If the username already exists update chattext. $updatechat = "UPDATE members SET chattext='chattext{$_POST['chattext']}'" }
__________________
subarus. |
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#7 |
Confirmed User
Join Date: Feb 2004
Location: Alberta, Canada
Posts: 4,707
|
so theres no way of only doing one query??
ie can you simply do something like $updatechat = "UPDATE members SET chattext='chattext{$_POST['chattext']}' WHERE user='$username'" and if it fails, execute $adduserquery = "INSERT INTO members (username,chattext) VALUES ('$username', '$chattext');
__________________
ICQ: 298-523-037 |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#8 |
Confirmed User
Join Date: May 2004
Location: SW Palm Bay, Florida
Posts: 1,323
|
yeah you could do that.
__________________
subarus. |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#9 |
Confirmed User
Join Date: May 2004
Location: SW Palm Bay, Florida
Posts: 1,323
|
Code:
$username = $_COOKIE['username']; //What user are we updating ? $chattext = $_POST['chattext']; //What text did this user submit ? //Let's check to see if this username is in the table. $usernamequery = "UPDATE members set chattext = 'chattext{$_POST['chattext']}' WHERE username='$username'"; $username = @mysql_query($usernamequery); $numusername = mysql_affected_rows($username);//Does this username exist ? if ($numusername = 0) {//If username doesn't exist add to table. $adduserquery = "INSERT INTO members (username,chattext) VALUES ('$username', '$chattext'); } Note the bold.
__________________
subarus. |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#10 |
Confirmed User
Join Date: Oct 2002
Posts: 252
|
Use MySQL 4.1 and you can do it all in one query. I use this for time costly reporting tasks.
INSERT INTO table VALUES ('user', 'text') ON DUPLICATE KEY UPDATE SET text = 'text'; Make sure you have a PRIMARY KEY or UNIQUE KEY on the user field. Anything under MySQL 4.1 isn't capable of this so you'll have to use multiple queries. |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#11 |
Confirmed User
Join Date: May 2004
Location: SW Palm Bay, Florida
Posts: 1,323
|
Nice thanks for that
![]()
__________________
subarus. |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#12 |
Confirmed User
Join Date: Oct 2002
Posts: 252
|
Btw, as of MySQL 4.1.1 you can also use VALUES(Column) in the ON DUPLICATE KEY UPDATE .. i.e.
INSERT INTO Reports VALUES (DATE(NOW()), 1) ON DUPLICATE KEY UPDATE ReportValue = VALUES(ReportValue) + 1; very useful - not to mention fast |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#13 |
Confirmed User
Join Date: Feb 2002
Location: Florida
Posts: 672
|
fuckin' A! you learn something new about this shit every day.
__________________
- this space intentionally left blank - |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#14 |
Confirmed User
Join Date: Feb 2004
Location: Alberta, Canada
Posts: 4,707
|
awesome
but i get an error.. INSERT INTO GUser VALUES (Myst, hello) ON DUPLICATE KEY UPDATE SET chattext = CONCAT(chattext,hello); You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SET chattext = CONCAT(chattext,hello)' at line 1 any ideas?
__________________
ICQ: 298-523-037 |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#15 |
Confirmed User
Join Date: Feb 2004
Location: Alberta, Canada
Posts: 4,707
|
nm i figured it out
silly me
__________________
ICQ: 298-523-037 |
![]() |
![]() ![]() ![]() ![]() ![]() |