SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot trigger8's Avatar
    Join Date
    Nov 2000
    Location
    Dallas, TX
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    insert data from ....

    How do I insert data from titletext1 and bodytext1 into combined1 (same to all the rows)?


    id title body combined
    -------------------------------------------
    1 titletext1 bodytext1 combined1
    2 titletext2 bodytext2 combined2
    3 . . .
    4 . . .
    Trigger8

    **************************
    I killed a 6-pack just to watch it die!

  2. #2
    Database Jedi MattR's Avatar
    Join Date
    Jan 2001
    Location
    buried in the database shell (Washington, DC)
    Posts
    1,107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What you want is to "update" each *existing* row in the database.

    this would be done something like this:

    Code:
    UPDATE table
       SET combined = title + body
    Or I think you may need to use the concat function.

  3. #3
    epsilon transition cupid's Avatar
    Join Date
    Aug 2001
    Location
    Kent, Ohio
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If there's a single operation in mysql that you can use like MattR had suggested (you can tell that my mysql knowledge is lacking ;p), you'd want to use that. The following is another approach.

    PHP Code:

    $query 
    mysql_query("select id,title,body from tablename");

    while (
    $result mysql_fetch_object($query)) {

      
    mysql_query("update tablename set combined1='".$result->title.$result.body."' where id='$result->id'");


    Last edited by cupid; Oct 8, 2001 at 15:09.

  4. #4
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this,
    PHP Code:
    $sql 'SELECT id,title,body FROM yourtable';

    $result = @mysql_query($sql):

    if (
    $result) {
      while (
    $row=mysql_fetch_array($result)) {
          
    $combined $row['title'].$row['body'];
          
    $sql "UPDATE table SET combined='$combined' WHERE ID=".$row['id'];
          if (@
    mysql_query($sql))
             echo 
    "Rows combined\n";
          else
             echo 
    "Database problem\n";
      }

    I'm assuming that by combined you mean just merge title and body fields. It's that not what you want, change line $combined = $row['title'].$row['body'];
    Last edited by Paul S; Oct 8, 2001 at 15:11.

  5. #5
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow, so many options

  6. #6
    SitePoint Zealot trigger8's Avatar
    Join Date
    Nov 2000
    Location
    Dallas, TX
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there anything wrong in the following script?

    PHP Code:
    <?
    MYSQL_CONNECT
    ($hostname$username$password) OR DIE( "Unable to connect to database");
    @
    mysql_select_db("$dbName") or die(  "Unable to select database"); 

    $sql ='SELECT id,Topic,Frequency,Heading FROM tablename'

    $result = @mysql_query($sql);

    if (
    $result) { 
    while (
    $row=mysql_fetch_array($result)) { 
    $combined $row['Heading'].$row['Topic'].$row['Frequency']; 
    $sql "UPDATE table SET keyword='$combined' WHERE ID=".$row['id']"; 
    if (@mysql_query(
    $sql)) 
    echo "
    Rows combined\n"; 
    else
    echo "
    Database problem\n";

    }
    ?>
    Trigger8

    **************************
    I killed a 6-pack just to watch it die!

  7. #7
    SitePoint Zealot trigger8's Avatar
    Join Date
    Nov 2000
    Location
    Dallas, TX
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now I got it right.

    But how do I seperate the values from each columns so that they don't really combined together.

    The output that I get now is 'text1text2text3'. How to make it 'text1 text2 text3'?

    Also, Is there anyway to only insert text without special characters..like \()_-...

    Thanks!
    Trigger8

    **************************
    I killed a 6-pack just to watch it die!

  8. #8
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    you can do it by adding,
    $combined = $row['Heading'].' '.$row['Topic'].' '.$row['Frequency'];


Bookmarks

Posting Permissions

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