How to retrive only the last inserted row from the database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mankolele
    New Member
    • Sep 2006
    • 63

    #1

    How to retrive only the last inserted row from the database

    Hi all. I am trying to get the input from a form to print it out on a page where the information filled on a form must be on the page in a letter manner.But now what is happening I do that but it displays everything in the database , while I only want the last entry to be displayed.To the letter.I dont have id on my table so cant use last_insert_id( )

    Any I dea to how I do that .Thanks in advance
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    There are a 100 ways to do this, but without a display of your code, it's no use. So show the code you are using now.

    Ronald :cool:

    Comment

    • mankolele
      New Member
      • Sep 2006
      • 63

      #3
      Originally posted by ronverdonk
      There are a 100 ways to do this, but without a display of your code, it's no use. So show the code you are using now.

      Ronald :cool:
      Here is my code....... It is supposed to come out in this letter and only the last entrys information needs to be filled in.The select part surely is wrong also coz I select * from the table not sure how to pu it .Thanx

      <?
      $username="root ";
      $password="issa sql";
      $database="asse ts";

      mysql_connect(l ocalhost,$usern ame,$password);
      @mysql_select_d b($database) or die( "Unable to select database");
      $query="SELECT * FROM product";
      $result=mysql_q uery($query);

      $num=mysql_numr ows($result);

      mysql_close();

      print '<b>I '.$issued_to.' hereby acknowledge full responsibilty for the equipment
      listed below. I acknowledge and fully understand that I will be held
      liable for any loss or damage (Software, Hardware or both) cuased to the
      equipment whilst in my possession.


      In the case of theft, loss or damage, I understand that I will be responsible for
      replacing the equipment lost, stolen or damage at their going
      market price.

      This equipment should be returned in the original condition as recieved.</b><br><br>

      <tr> <tr>
      <td> <td>
      TRANSFERED FROM</b><br><br> TRANSFERED TO</b><br><br>
      Responsible Person:</b><br><br> Responsible Person:</b><br><br>
      Section:</b><br><br> Section:</b><br><br>
      Signature:</b><br><br> Signature:</b><br><br>
      Date:</b><br><br></td></tr> Date:</b><br><br></td></tr>';

      $i=0;
      while ($i < $num) {

      $description=my sql_result($res ult,$i,"descrip tion");
      $serial_number= mysql_result($r esult,$i,"seria l_number");
      $unique_dept_nu m=mysql_result( $result,$i,"uni que_dept_num");
      $issued_to=mysq l_result($resul t,$i,"issued_to ");
      $issued_from=my sql_result($res ult,$i,"issued_ from");
      $section=mysql_ result($result, $i,"section");
      $issue_date=mys ql_result($resu lt,$i,"issue_da te");
      $return_date=my sql_result($res ult,$i,"return_ date");

      echo "<b>Descrip tion :$description </b><br>Serial Number:$serial_ number</b><br>Unique Department Number:$unique_ dept_num<br>Iss ued To: $issued_to<br>I ssued From: $issued_from<br >Employee Section: $section<br>Dat e of Issue: $issue_date<br> Return Date: $return_date<hr ><br>";

      $i++;
      }

      ?>

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        After 20 or so posts in this forum you know. you have been told before, that you should put your code with php, code or html tags! as stated in the Posting Guidelines.

        If you want to seek help in this forum, all we ask is that you comply with a few simple rules. People who continuously refuse to do just that, and consider this forum a one-way street for their use, will not be answered by me.

        Ronald :cool:

        Comment

        • mankolele
          New Member
          • Sep 2006
          • 63

          #5
          Originally posted by ronverdonk
          After 20 or so posts in this forum you know. you have been told before, that you should put your code with php, code or html tags! as stated in the Posting Guidelines.

          If you want to seek help in this forum, all we ask is that you comply with a few simple rules. People who continuously refuse to do just that, and consider this forum a one-way street for their use, will not be answered by me.

          Ronald :cool:
          [php]
          $username="root ";
          $password="issa sql";
          $database="asse ts";

          mysql_connect(l ocalhost,$usern ame,$password);
          @mysql_select_d b($database) or die( "Unable to select database");
          $query="SELECT * FROM product";
          $result=mysql_q uery($query);

          $num=mysql_numr ows($result);

          mysql_close();

          print '<img src="3dcoatl.jp g">



          [html]
          &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;<h2>CERTI FICATE FOR THE TRANSFER OF ASSETS/EQUIPMENT</br>
          TRANSFERED FROM ONE PERSON TO ANOTHER </h2>




          <b>I '.$issued_to.' hereby acknowledge full responsibilty for the equipment
          listed below. I acknowledge and fully understand that I will be held
          liable for any loss or damage (Software, Hardware or both) cuased to the
          equipment whilst in my possession.


          In the case of theft, loss or damage, I understand that I will be responsible for
          replacing the equipment lost, stolen or damage at their going
          market price.

          This equipment should be returned in the original condition as recieved.</b><br><br>

          <table width="100%" border="1">
          <tr>
          <td>Description </td>
          <td>Serial Number</td>
          <td>Unique Dept No</td>
          <td>Quantity</td>
          </tr>

          <tr>
          <td>$descriptio n </td>
          <td>$serial_num ber</td>
          <td>$unique_dep t_num</td>
          <td>$quantity </td>
          </tr>
          <tr>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          </tr>
          <tr>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          </tr>
          <tr>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          </tr>
          <tr>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          </tr>
          <tr>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          </tr>
          </table>
          &nbsp;
          &nbsp;
          &nbsp;
          &nbsp;
          &nbsp;
          &nbsp;
          &nbsp;
          <table width="100%" border="0">
          <tr>
          <td>TRANSFERE D FROM</td>
          <td>TRANSFERE D TO</td>
          </tr>
          <tr>
          <td>Responsib le Person:</td>
          <td>Responsib le Person:</td>
          </tr>
          <tr>
          <td>Section:</td>
          <td>Section:</td>
          </tr>
          <tr>
          <td>Signature :</td>
          <td>Signature :</td>
          </tr>
          <tr>
          <td>Date:</td>
          <td>Date:</td>
          </tr>
          </table>';
          [/html]

          if ($num>1) {
          $to=1;
          }else{
          $to=$num;
          }



          $i=0;
          while ($i < $to) {

          $description=my sql_result($res ult,$i,"descrip tion");
          $serial_number= mysql_result($r esult,$i,"seria l_number");
          $unique_dept_nu m=mysql_result( $result,$i,"uni que_dept_num");
          $issued_to=mysq l_result($resul t,$i,"issued_to ");
          $issued_from=my sql_result($res ult,$i,"issued_ from");
          $section=mysql_ result($result, $i,"section");
          $issue_date=mys ql_result($resu lt,$i,"issue_da te");


          //echo "<b>Descrip tion :$description </b><br>Serial Number:$serial_ number</b><br>Unique Department Number:$unique_ dept_num<br>Iss ued To: $issued_to<br>I ssued From: $issued_from<br >Employee Section: $section<br>Dat e of Issue: $issue_date<hr> <br>";
          $i++;
          }

          [/php]

          hope this looks fine

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #6
            This should be easy, but that depends entirely on how to distinguish the last row inserted in the table. I also have some remarks and some errors in your code, but we will get to that after you have replied.

            The big question here for you is:
            How can you recognise the last row inserted in the table? You said there is no id (I assume you meant no auto_increment field), but is there maybe a timestamp, or ANY field that you can use to determine the last row inserted?
            If you don't have such a field, how else can you determine what the last one is?

            Ronald :cool:

            Comment

            • mankolele
              New Member
              • Sep 2006
              • 63

              #7
              Originally posted by ronverdonk
              This should be easy, but that depends entirely on how to distinguish the last row inserted in the table. I also have some remarks and some errors in your code, but we will get to that after you have replied.

              The big question here for you is:
              How can you recognise the last row inserted in the table? You said there is no id (I assume you meant no auto_increment field), but is there maybe a timestamp, or ANY field that you can use to determine the last row inserted?
              If you don't have such a field, how else can you determine what the last one is?

              Ronald :cool:
              Yes there is no auto_increment, not sure what a time stamp is maybe it could work,the reason I do not have the auoto increment is the unique_dept_num is already a PK so is there a way I could use? I will be happy to know I cant use the auto_increment always.

              Comment

              • gm04030276
                New Member
                • Nov 2006
                • 17

                #8
                the easiest way is to have a field, i just call it ID, and auto increment it and then you can either run a little while loop that reads out the ID field until it finds the bigest value although thinking about it that might not work, id have to think about that. but i will asume there is an sql function that will tell you how many records there are in a table so you then simply use that value and go

                [PHP]
                $resource = mysql_query('SE LECT * FROM `table` WHERE ID = ' . $sql_record_num . ';', $link);
                [/PHP]

                Comment

                • theRamones
                  New Member
                  • Nov 2006
                  • 13

                  #9
                  may this help
                  [php]
                  $row = mysql_query("se lect count(*) from product");
                  $count = $row[0];
                  // to get last inserted row :
                  $count -= 1; //last inserted row index
                  $lastRow = mysql_query("se lect * from product limit $count, 1");
                  [/php]

                  Comment

                  • mankolele
                    New Member
                    • Sep 2006
                    • 63

                    #10
                    Originally posted by theRamones
                    may this help
                    [php]
                    $row = mysql_query("se lect count(*) from product");
                    $count = $row[0];
                    // to get last inserted row :
                    $count -= 1; //last inserted row index
                    $lastRow = mysql_query("se lect * from product limit $count, 1");
                    [/php]
                    Thanx for the replies.

                    Decided to change the database structure to use id as my primary key. How do I use the id to get the last inserted row information??

                    Comment

                    • ronverdonk
                      Recognized Expert Specialist
                      • Jul 2006
                      • 4259

                      #11
                      Since the ID is always incremented when a row is stored, the highest id will be the last. You can get this by:
                      [php]
                      SELECT * from table ORDER BY id DESC LIMIT 1;
                      [/php]
                      Ronald :cool:

                      Comment

                      Working...