SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot dbworm's Avatar
    Join Date
    Apr 2003
    Location
    Lausanne
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Updating a field in a loop?

    I can't get this to work. How do I tell the query to update the current row?
    PHP Code:
    <?php
    $conn 
    mysql_connect('','','');
    mysql_select_db('test'$conn);
    $result mysql_query('select colour_name from colours'$conn);

    while (
    $row mysql_fetch_row($result))
    {
     
    $concatname str_replace(" """$row[0]);
     
    mysql_query('update colours set colour_name=' $concatname);
    }
    ?>
    No error returned but no updating either...

    Thanks, Dan

  2. #2
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)

  3. #3
    SitePoint Zealot dbworm's Avatar
    Join Date
    Apr 2003
    Location
    Lausanne
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DJ P@CkMaN
    If this is what you mean, it doesn't work either:
    PHP Code:
    <?php 
    $conn 
    mysql_connect('','',''); 
    mysql_select_db('test'$conn); 
    $result mysql_query('select colour_name from colours'$conn); 

    while (
    $row mysql_fetch_row($result)) 

    $concatname str_replace(" """$row[0]); 
    mysql_query('update colours set colour_name = ' $concatname ' where colour_name = ' $row[0]);

    ?>

  4. #4
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Add an or die(mysql_error()) to your queries, to see the mysql error message:

    PHP Code:
    <?php
    $conn 
    mysql_connect('','','');
    mysql_select_db('test'$conn);
    $result mysql_query('select colour_name from colours'$conn) or die(mysql_error());

    while (
    $row mysql_fetch_row($result))
    {
    $concatname str_replace(" """$row[0]);
    mysql_query('update colours set colour_name = ' $concatname ' where colour_name = ' $row[0]) or die(mysql_error());
    }
    ?>

  5. #5
    SitePoint Zealot dbworm's Avatar
    Join Date
    Apr 2003
    Location
    Lausanne
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Chris82
    Add an or die(mysql_error()) to your queries, to see the mysql error message:
    I just forgot to single quote the values. Should be:
    PHP Code:
    mysql_query('update colours set colour_name = \'' $concatname '\' where colour_name = \'' $row[0] . '\'') or die(mysql_error()); 
    Aaaargh...

    Thanks a bunch, Dan


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
  •