Results 1 to 2 of 2

Thread: Problem with second query ...

  1. #1
    Join Date
    Nov 2008
    Posts
    44
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Problem with second query ...

    Hello:

    I am having a problem and I am not sure my method is correct. I know my sub-query isn't. What I need is to get the Max-Autonum, or last record for a specific record. Please see the example for a better understanding:



    part_id_num part_manufac_code part_number
    1 36 18547
    2 36 57847
    3 35 79811
    4 36 57914
    5 35 74992


    If I am looking for part_manufac_code 36, then the last record (part_id_num) for that is 4. Here is the code that I have:
    //
    //
    //
    require("connect.php");
    //
    //
    //
    $query1 = "SELECT part_id_num, part_manufac_code, part_number FROM tblparts WHERE (part_manufac_code = '$frmfindpart')";
    $result1 = mysql_query($query1) or die(mysql_error());
    //
    //
    $query2 = "SELECT MAX(part_id_num) FROM '$result1'";
    $result2 = mysql_query($query2) or die(mysql_error());
    if(mysql_num_rows($result2) > 0)
    {
    while($row = mysql_fetch_assoc($result2))
    {
    $names[] = $row['name'];
    }
    }
    else
    {
    echo 'query returned no results';
    }

  2. #2
    Join Date
    Oct 2008
    Posts
    60
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default

    Try querying using what you have
    PHP Code:
    $query1 "SELECT part_id_num, part_manufac_code, part_number FROM tblparts WHERE (part_manufac_code = '$frmfindpart')";
    $result1 mysql_query($query1) or die(mysql_error()); 
    except add limit and ASC or DESC
    PHP Code:
    $query1 "SELECT part_id_num, part_manufac_code, part_number FROM tblparts WHERE (part_manufac_code = '$frmfindpart') LIMIT 1 DESC "
    This way you always get the last row using the part_manufac_code, as long as this is what you want. Other than that, you should use the part_number or ID to get specific queries rather then going the long and hard way about this.

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
  •