update column based on another column with custom php function

Discussion in 'MySQL' started by bumbar, Nov 27, 2013.

  1. #1
    Hello!

    I have table with huge record. I have two columns name_bg and name_en.
    I added more two columns ..... ident_bg and ident_en.
    But they must have data based on data in name_bg and name_en with some change data.

    Here mysql statement who work:

    UPDATE firms tu, firms ts
    SET
        tu.ident_bg =ts.name_bg,
        tu.ident_en = ts.name_en
    WHERE tu.id=ts.id
    Code (markup):
    But i have function which must be applied to the columns ts.name_bg and ts.name_en.
    i.e.
    Slug::slugify(ts.name_bg)
    Code (markup):
    Slug::slugify(ts.name_en)
    Code (markup):
    I do not know how to properly apply the column to the function as an argument

    i.e.

    UPDATE firms tu, firms ts 
    SET
        tu.ident_bg =Slug::slugify(ts.name_bg),
        tu.ident_en =Slug::slugify(ts.name_en)
    WHERE tu.id=ts.id
    Code (markup):
    This of course does not work.

    How to do work?
    Thanks!
     
    Solved! View solution.
    bumbar, Nov 27, 2013 IP
  2. #2
    Can't you just run a select statement first, and then update through a loop based on that?
    
    $sql = "SELECT id, name_bg, name_en FROM firms";
    $stmt = $dbh->query($sql);
    while ($row = $stmt->fetch()) {
      $id = $row['id']
      $name_bg = Slug::slugify($row['name_bg']);
      $name_en = Slug::slugify($row['name_en']);
      $sql = "UPDATE firms SET ident_bg = :name_bg, ident_en = :name_en WHERE id = :id";
      $stmt = $core->dbh->prepare($sql);
      $stmt->execute(array(':name_bg'=>$name_bg,':name_en'=>$name_en,':id'=>$id));
    }
    
    PHP:
    //note that I use PDO within PHP, so the above example assumes you have $dbh assigned to the PDOstatement. (New PDO).
     
    PoPSiCLe, Nov 27, 2013 IP
  3. bumbar

    bumbar Active Member

    Messages:
    68
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #3
    Thanks, that's the idea.
    But when loading the script get update only the first record ....
     
    bumbar, Nov 27, 2013 IP