Replace doesnt seem to be working why?

Discussion in 'MySQL' started by light766, Sep 18, 2011.

  1. #1
    im not a pro on this but why doesnt this work?

    $SQL = "REPLACE INTO Levels SET id = '$id' AND levelname = '$levelname', walls = '$walls' AND floors = '$floors';"; :confused:
     
    light766, Sep 18, 2011 IP
  2. rayqsl

    rayqsl Active Member

    Messages:
    91
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    53
    #2
    If you're trying to replace data in an existing row, you need to use UPDATE. So your code would become $SQL = "UPDATE Levels SET id = '$id' AND levelname = '$levelname', walls = '$walls' AND floors = '$floors';";

    But you need to be careful here because there is no WHERE clause so the update would change every row in the table.

    You use INTO with an INSERT command.
     
    rayqsl, Sep 18, 2011 IP
  3. light766

    light766 Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    well i want it to check if it already exists, and if it doesnt then it will create a row, and if it does it will replace the data?
    am i taking the right approach?
     
    light766, Sep 18, 2011 IP
  4. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #4
    No. Select the row. If you get one, update it. If you don't, insert one. There's no "replace if it exists, otherwise create one" SQL syntax.
     
    Rukbat, Sep 18, 2011 IP
  5. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #5
    There is Replace in MySQL. Refer it here.

    You can also try INSERT INTO .. ON DUPLICATE KEY UPDATE. refer this.
     
    mastermunj, Sep 19, 2011 IP
  6. light766

    light766 Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I did an select all, then if any rows will update, else will insert.
    is doing replace necessarily "better" when it comes to bandwith and such
     
    light766, Sep 19, 2011 IP
  7. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #7
    Website bandwidth has nothing to do with mysql query as long as data retrieved by mysql is not transferred at client end.

    Queries run solely on server, mostly same server on shared host. If you have 2 separate server for front-end and back-end, and doing single select and insert, then of course network usage can happen, however that again is not the end-user bandwidth utilization.
     
    mastermunj, Sep 21, 2011 IP