我正在尝试使此脚本工作,但向我展示了此问题

时间:2019-04-17 09:39:37

标签: php mysqli

  

警告:mysqli_query()期望参数2为字符串,对象在C:\ xampp \ htdocs中给出

/**
    * Build new customer number
    * @return varchar custNo :  Newly build customer number
    */  
    function buildCustNo(){ 
    /*Establish Database connection
    */
        $link = mysqli_connect("localhost","root","","mysaccodb") or die("Unable to Connect to Database");      


    // Determine biggest customer ID
    $sql_maxID = mysqli_query($link,"SELECT MAX(cust_id) AS maxid FROM customer") or die ("Error" . mysqli_error($link));
    $query_maxID = mysqli_query($link, $sql_maxID);
    checkSQL($query_maxID);
    $result_maxID = mysqli_fetch_array($query_maxID);

    // Read customer number format
    $cnParts = explode("%", $_SESSION['set_cno']);
    $cnCount = count($cnParts);

    // Build customer number
    $i = 0;
    $custNo = "";
    for ($i = 1; $i < $cnCount; $i++) {
        switch($cnParts[$i]){
            case "N":
                $custNo = $custNo.($result_maxID['maxid'] + 1);
                break;
            case "Y":
                $custNo = $custNo.date("Y",time());
                break;
            case "M":
                $custNo = $custNo.date("m",time());
                break;
            case "D":
                $custNo = $custNo.date("d",time());
                break;
            default:
                $custNo = $custNo.$cnParts[$i];
        }
    }

    // Return customer number
    return $custNo;
}

1 个答案:

答案 0 :(得分:1)

您正在mysqli_query中使用mysqli_query,但无法正常工作。看看下面的样子。

/**
* Build new customer number
* @return varchar custNo :  Newly build customer number
*/  
function buildCustNo(){ 
/*Establish Database connection
*/
    $link = mysqli_connect("localhost","root","","mysaccodb") or die("Unable to Connect to Database");      


// Determine biggest customer ID// Determine biggest customer ID
$sql_maxID = "SELECT MAX(cust_id) AS maxid FROM customer";
$query_maxID = mysqli_query($link, $sql_maxID)  or die ("Error" . mysqli_error($link));

checkSQL($query_maxID);
$result_maxID = mysqli_fetch_array($query_maxID);

// Read customer number format
$cnParts = explode("%", $_SESSION['set_cno']);
$cnCount = count($cnParts);

// Build customer number
$i = 0;
$custNo = "";
for ($i = 1; $i < $cnCount; $i++) {
    switch($cnParts[$i]){
        case "N":
            $custNo = $custNo.($result_maxID['maxid'] + 1);
            break;
        case "Y":
            $custNo = $custNo.date("Y",time());
            break;
        case "M":
            $custNo = $custNo.date("m",time());
            break;
        case "D":
            $custNo = $custNo.date("d",time());
            break;
        default:
            $custNo = $custNo.$cnParts[$i];
    }
}

// Return customer number
return $custNo;
}