SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jun 2001
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    mysql update by row number?

    This may be a truly dumb question, but it's baffling me...

    Can anyone tell me how to update a value in field1 of rowN of a table, whether or not the table has a primary key field? For example, something like:

    UPDATE mytable SET field1="Joe" WHERE row???=24

    Is there a system variable for row?

    I'm new at mysql but an aged veteran of dBase years ago, which had a recno() variable -- essentially the row number. I may not need this in the long run but am working on a script to edit and update any table I might want to create, keyed or not.

  2. #2
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That I know of, there isn't any 'variable' for like rows. However you can get the number of rows in a table.

    mysql_num_rows()

    Generally when you are updating, you are updating a row where you already have the value of something in it, that will not change. So it would be like:
    UPDATE mytable SET field1='Joe' WHERE id='$id'

    The id would never change and it would probably be a primary key.

    Since you are working on a script to update any table, you wouldn't want to have to specify the primary key or an element in each separate query for which to leave alone and reference from.
    So perhaps something like this:
    UPDATE $mytable SET field1='Joe' WHERE $uniquecolumn='$variable'

    This way you could apply this to any table, and just define what $uniquecolumn and $variable are before hand, so they can act like primary keys for you.

  3. #3
    SitePoint Member
    Join Date
    Jun 2001
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks much, Timnz! A bit of extra work to find the primary key in a table, but doable. I appreciate knowing that I won't be doing things an extra hard way.


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
  •