1

是否可以使用 Ajax 从 MySql 数据库中获取最后一行?

所以这是我的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);

?>

这是阿贾克斯:

$(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);
      } 
    });
  }); 

所以基本上我想获取最后一个 id 并自动增加它,上面只增加数据库中的第一行。

任何帮助或建议,将不胜感激!

4

3 回答 3

5

只需通过按主键降序添加订单并将其限制为仅第一个结果:

SELECT * FROM TableName ORDER BY TableId DESC LIMIT 1
于 2013-07-29T12:17:11.780 回答
0

尝试按 desc 排序。您将获得表格的最后一行

句法

SELECT *
FROM table_name
ORDER BY column1, column2 

例如:-

SELECT * FROM `Tbl_name` ORDER BY 'id' DESC LIMIT 1
于 2013-07-29T12:20:08.353 回答
0

使用 ajax 并保持 PHP 的相同版本:

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

但我更愿意更改您的 ajax 函数,以便它了解您要执行的操作并通过 PHP 返回最后一个 id。如果您只想要最后一个 id,但您正在检索数据库中的所有数据,那有点矫枉过正。

于 2013-07-29T12:22:49.830 回答