Ajax:从数据库中获取最后一行

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!

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

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

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.