SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Sep 2002
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with SELECT MAX() command.

    Code:
    $sql = mysql_query("SELECT MAX(id) as max_id FROM f_members");
    $result = mysql_fetch_row($sql);
    $user_id = $result['max_id'] + 1;
    
    echo $user_id;
    Is my syntax right? Cause all I keep getting is the number 1. Even tho my max number should be 4 + 1.
    :blink:

  2. #2
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First of all; I think you have mixed up the variable names
    This makes more sense:
    $sql = "SELECT MAX(id) as max_id FROM f_members";
    $result = mysql_query($sql);
    $row = mysql_fetch_row($result);
    $user_id = $row['max_id'] + 1;
    echo $user_id;

    Second, change
    $result = mysql_query($sql);
    to
    $result = mysql_query($sql) or die(mysql_error());

    Then you will see why $result is empty
    e.g. "Table doesn't exist"

    Is there an open connection? (mysql_connect())
    Is the right db selected? (mysql_select_db())

  3. #3
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I guess you want to use (max_id + 1) as a new id for the next user to insert (correct?)
    Why don't you use auto_increment for the id column?


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
  •