I have a table for users payments. I need to specify a receipt code for each transaction, so I need to know what was the last one? And if there is not previous receipt, I want to add the first one with 1000 and other reciepts must be incremental like 1001, 1002, …
For this, I wrote this code:
$receipt = '1000';
$query_receipt = mysql_query("SELECT MAX(receipt) FROM tbl_payments");
$max = mysql_result($query_receipt,0);
if ($max != '0' || $max != '')
{
$receipt = mysql_result($query_receipt,0) + 1;
}
It works fine when I have a record in the table. But it returns 1 when table is empty. How can I solve this problem? I need to know the returned value from a SELECT MAX() ... even if table be empty.
You can achive this by using
IFNULLas: