是否可以使用Ajax從MySql數據庫獲取最後一行?Ajax:從數據庫獲取最後一行
所以這是我的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,並自動增加它,上面只增加第一行在數據庫中。
任何幫助或建議,將不勝感激!按降序
SELECT * FROM TableName ORDER BY TableId DESC LIMIT 1
你想要什麼。這是表最後一行嗎? –
'最後一行'是什麼?最後一次修改行?最下面的那排? - 請解釋。 –
你需要在哪裏使用最後一個ID?自動增量是一個數據庫功能,你永遠不想這樣做你自己,這是一個手動增量.. – Garytje