Need some simple mysql help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Myst
    Confirmed User
    • Feb 2004
    • 4708

    #1

    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
  • Myst
    Confirmed User
    • Feb 2004
    • 4708

    #2
    come on boys this is elementary
    ICQ: 298-523-037

    Comment

    • calmlikeabomb
      Confirmed User
      • May 2004
      • 1323

      #3
      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.

      Comment

      • calmlikeabomb
        Confirmed User
        • May 2004
        • 1323

        #4
        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.

        Comment

        • Myst
          Confirmed User
          • Feb 2004
          • 4708

          #5
          im trying to append it
          but i dont know much about mysql, so i thought i need the update command..
          ICQ: 298-523-037

          Comment

          • calmlikeabomb
            Confirmed User
            • May 2004
            • 1323

            #6
            For every message received, what would be the best way of updaitng the table?
            You can do something to this effect....
            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']}'"
            }
            In this example a form is being used to pass the values to the variables.
            Last edited by calmlikeabomb; 02-10-2005, 07:09 PM.
            subarus.

            Comment

            • Myst
              Confirmed User
              • Feb 2004
              • 4708

              #7
              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

              Comment

              • calmlikeabomb
                Confirmed User
                • May 2004
                • 1323

                #8
                yeah you could do that.
                subarus.

                Comment

                • calmlikeabomb
                  Confirmed User
                  • May 2004
                  • 1323

                  #9
                  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 = "[b]UPDATE members set chattext = 'chattext{$_POST['chattext']}' WHERE username='$username'[/b]";
                  $username = @mysql_query($usernamequery);
                  $numusername = [b]mysql_affected_rows[/b]($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');
                  }
                  It's pretty much the same on system resources needed.....

                  Note the bold.
                  Last edited by calmlikeabomb; 02-10-2005, 07:49 PM.
                  subarus.

                  Comment

                  • Dragon Curve
                    Confirmed User
                    • Oct 2002
                    • 252

                    #10
                    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.

                    Comment

                    • calmlikeabomb
                      Confirmed User
                      • May 2004
                      • 1323

                      #11
                      Nice thanks for that
                      subarus.

                      Comment

                      • Dragon Curve
                        Confirmed User
                        • Oct 2002
                        • 252

                        #12
                        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

                        Comment

                        • Cogitator
                          Confirmed User
                          • Feb 2002
                          • 672

                          #13
                          fuckin' A! you learn something new about this shit every day.
                          - this space intentionally left blank -

                          Comment

                          • Myst
                            Confirmed User
                            • Feb 2004
                            • 4708

                            #14
                            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

                            Comment

                            • Myst
                              Confirmed User
                              • Feb 2004
                              • 4708

                              #15
                              nm i figured it out
                              silly me
                              ICQ: 298-523-037

                              Comment

                              Working...