+ Post New Thread
Results 1 to 4 of 4
Coding Thread, Mysql - pre-pend to string in Coding and Web Development; I have a table full of statements teachers use to write there reports. they want all statements to begin with ...
  1. #1

    Join Date
    Apr 2006
    Location
    UK
    Posts
    939
    Thank Post
    39
    Thanked 70 Times in 54 Posts
    Rep Power
    30

    Mysql - pre-pend to string

    I have a table full of statements teachers use to write there reports. they want all statements to begin with the students name (this code has been sorted) but to do this i need all the statements to start with '--fristname'

    How can i add '--firstname' to the beginning of all the strings stored in the table?

  2. #2

    powdarrmonkey's Avatar
    Join Date
    Feb 2008
    Location
    Alcester, Warwickshire
    Posts
    4,866
    Thank Post
    412
    Thanked 777 Times in 650 Posts
    Rep Power
    182
    Assumption: each row has a unique id, and you have already established a connection called db.

    (disclaimer: I haven't tested this.)

    Code:
    $sub = '--firstname';
    $table = 'mytable';
    $getsql = "SELECT id, name FROM  {$table}";
    
    $getresult = mysql_query($getsql, $db);
    
    while($row = mysql_fetch_array($getresult))
    {
        $sql = "UPDATE {$table} SET `name`='{$sub}{$row[1]}' WHERE `id`=${row[0]} LIMIT 1";
        if( ! mysql_query($sql, $db) )
            print("Unable to update row {$row[0]}\n";
    }

  3. #3

    webman's Avatar
    Join Date
    Nov 2005
    Location
    North East England
    Posts
    8,413
    Thank Post
    642
    Thanked 964 Times in 664 Posts
    Blog Entries
    2
    Rep Power
    327
    OK, try this on a test table first (using Concat) :

    Code:
    UPDATE statements SET statement = CONCAT('--firstname', statement)
    Last edited by webman; 9th January 2009 at 07:17 PM. Reason: Mis-read original post, oops!

  4. 2 Thanks to webman:

    danIT (9th January 2009), powdarrmonkey (9th January 2009)

  5. #4

    powdarrmonkey's Avatar
    Join Date
    Feb 2008
    Location
    Alcester, Warwickshire
    Posts
    4,866
    Thank Post
    412
    Thanked 777 Times in 650 Posts
    Rep Power
    182
    Thought it could probably do something like that, but I've just never had time to learn

SHARE:
+ Post New Thread

Similar Threads

  1. Pre Logon Messages
    By FN-GM in forum Windows Vista
    Replies: 3
    Last Post: 30th July 2008, 08:31 PM
  2. Pre-terminated fibre: ST-ST or SC-SC?
    By dhicks in forum Hardware
    Replies: 15
    Last Post: 23rd May 2008, 09:46 PM
  3. Pre-N
    By binky in forum Hardware
    Replies: 3
    Last Post: 3rd March 2008, 10:42 AM
  4. Replies: 31
    Last Post: 2nd November 2007, 06:20 PM
  5. need to extract text from a string in excel
    By projector1 in forum How do you do....it?
    Replies: 7
    Last Post: 14th February 2007, 02:41 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •