1

Is it possible to get the last row from MySql database using Ajax?

So this is my PHP:

<?php 
  include('../../dbconn.php');
  //--------------------------------------------------------------------------
  // Example php script for fetching data from mysql database
  //--------------------------------------------------------------------------
  $databaseName = "test";
  $tableName = "generalTransactions";

  //--------------------------------------------------------------------------
  // 1) Connect to mysql database
  //--------------------------------------------------------------------------

  $con = mysql_connect($gaSql['server'],$gaSql['user'],$gaSql['password']);
  $dbs = mysql_select_db($databaseName, $con);
  //--------------------------------------------------------------------------
  // 2) Query database for data
  //--------------------------------------------------------------------------

  $result = mysql_query("SELECT * FROM $tableName");          //query
  $array = mysql_fetch_row($result);                          //fetch result                          
  //--------------------------------------------------------------------------
  // 3) echo result as json 
  //--------------------------------------------------------------------------
  echo json_encode($array);

?>

This is the Ajax:

$(function () 
  {
    //-----------------------------------------------------------------------
    // 2) Send a http request with AJAX http://api.jquery.com/jQuery.ajax/
    //-----------------------------------------------------------------------
    $.ajax({                                      
      url: 'general/transactions/add-journal.php',                  //the script to call to get data          
      data: '',                        //you can insert url argumnets here to pass to api.php
                                       //for example "id=5&parent=6"
      dataType: 'json',                //data format      
      success: function(data)          //on recieve of reply
      {
        var id = data[0];
        var prefix = data[1];                //get prefix
        var journalNum = data[2];            //get journal number 
        //--------------------------------------------------------------------
        // 3) Update html content
        //--------------------------------------------------------------------
        //recommend reading up on jquery selectors they are awesome 
        // http://api.jquery.com/category/selectors/
        if(!journalNum){
            $('#generalJournalNumber').val('GJ00000001');
        }
        id++;
        $('#generalJournalNumber').val(prefix+'0000000'+id);
      } 
    });
  }); 

So basically I want to get the last id and auto increment it, the above only increments the first row in the database.

Any help or suggestions, will be appreciated!

6
  • What do you want. is this Table last row? Commented Jul 29, 2013 at 12:15
  • What's the last row? The last modified row? The row at the very bottom? - Please explain. Commented Jul 29, 2013 at 12:16
  • where do you need the last id for? auto increment is a database functionality, you never want to do this your self, that's a manual increment.. Commented Jul 29, 2013 at 12:16
  • @Garytje I'm not going to try auto increment the id in the database, this is for display purposes only.. Commented Jul 29, 2013 at 12:17
  • SELECT MAX(id) FROM table, or SELECT id FROM table ORDER BY id DESC LIMIT 1 Commented Jul 29, 2013 at 12:18

3 Answers 3

5

Just add an order by descending on your primary key and limit it to only the first result:

SELECT * FROM TableName ORDER BY TableId DESC LIMIT 1
Sign up to request clarification or add additional context in comments.

3 Comments

He want Is it possible to get the last row from MySql database using Ajax?.
@MaciejCzyżewski use ajax to call php page which will internally call this select statemtent
It works because it is selecting the ids from largest to smallest and returning only the top one - i.e. the largest id in the table
0

Try order by desc. You will get last row of the table

Syntax

SELECT *
FROM table_name
ORDER BY column1, column2 

Eg :-

SELECT * FROM `Tbl_name` ORDER BY 'id' DESC LIMIT 1

Comments

0

using ajax and keeping the same version of your PHP:

var id = data[data.length-1];

but i would prefer changing your ajax function so that it would understand what you are trying to do and would return the last id via PHP. If you just want the last id, but you are rertrieving all the data in your database, that's a little bit of an overkill.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.