PHP函数返回相同的值

时间:2017-06-07 20:10:31

标签: php

我已经定义了一个非常好的PHP函数,但是我遇到了一个问题。我试图在具有不同值的同一页面上多次调用GenerateIDNumber函数,但它返回第一次定义的变量的相同值,是否有任何方法close/end函数并用新值调用它。

这是我的代码

<?php
/* GenerateID Number */
function GenerateIDNumber($tablenames, $columnnames)
{
    global $conn;
    global $highest_id;

    $sql    = "SELECT MAX(" . $columnnames . ") as max FROM " . $tablenames;
    $rs     = mysqli_query($conn, $sql);
    $result = mysqli_fetch_row($rs);
    if ($result[0] >= 1) {
        $highest_id = $result[0] + 1;
    } else {
        $highest_id = 1;
    }
}
/* GenerateID Number */

$highest_id    = null;
$mytable_name  = "tblleasesalespayment";
$mycolumn_name = "PaymentID";
GenerateIDNumber($mytable_name, $mycolumn_name);
$printNumber     = $highest_id;
$SaleIDGenerator = "CRV-";
$SetNumberCust   = $SaleIDGenerator . $_SESSION['ACTBRANCHID'] . date('y') . sprintf("%08s", $printNumber);

$Code1 = $SetNumberCust;

$highest_id    = null;
$mytable_name  = "tblrepossess_vehicles";
$mycolumn_name = "rp_id";
new GenerateIDNumber($mytable_name, $mycolumn_name);
$printNumber   = $highest_id;
$SetNumberCust = "REP-" . $_SESSION['ACTBRANCHID'] . date('y') . sprintf("%08s", $printNumber);
$Code2         = $SetNumberCust;
?> 

1 个答案:

答案 0 :(得分:1)

检查这是否为您的请求提供服务

/* GenerateID Number */
function GenerateIDNumber($tablenames, $columnnames)
{
    global $conn;

    $sql    = "SELECT MAX(" . $columnnames . ") as max FROM " . $tablenames;
    $rs     = mysqli_query($conn, $sql);
    $result = mysqli_fetch_row($rs);
    if ($result[0] >= 1) {
        $highest_id = $result[0] + 1;
    } else {
        $highest_id = 1;
    }
    return $highest_id ;
}



 $highest_id    = null;
$mytable_name  = "tblleasesalespayment";
$mycolumn_name = "PaymentID";
$highest_id = GenerateIDNumber($mytable_name, $mycolumn_name);
$printNumber     = $highest_id;
$SaleIDGenerator = "CRV-";
$SetNumberCust   = $SaleIDGenerator . $_SESSION['ACTBRANCHID'] . date('y') . sprintf("%08s", $printNumber);

echo $Code1 = $SetNumberCust. " As Code1 value <br />";

$highest_id    = null;
$mytable_name  = "tblrepossess_vehicles";
$mycolumn_name = "rp_id";
$highest_id = GenerateIDNumber($mytable_name, $mycolumn_name);
$printNumber   = $highest_id;
$SetNumberCust = "REP-" . $_SESSION['ACTBRANCHID'] . date('y') . sprintf("%08s", $printNumber);
echo $Code2 = $SetNumberCust. " As Code2 value <br />";