php – How to handle a Function utilizing a Stored Procedure from an array of data-ThrowExceptions

Exception or error:

I have created a function that checks for quantities before saving the data into the sales table. This function works well on Single variables and the moment the post variables become multiple; it doesn’t work because they become an array. Is there a way in which I can handle an array utilizing the idea behind this function? The idea is for every set say ($productCode = 2) & ($quantity = 10) function checks, then say ($productCode = 1) & ($quantity = 20) until the last set.

            //Checking quantity availability
            //Post variables

            //Equivalent variables
            $productCode = array(2, 1, 7, 2);
            $quantity    = array(10, 20, 30, 40);

            //My wild thought BUT no LUCK
            for ($i = 0; $i < count($productCode); $i++) {

            function CheckIfQuantityIsEnough ($productCode,$quantity)
                    $QuantityIsEnough = false;
                    $pdoAssigned = new PDOConfig;
                    //Works perfectly well for one variable set
                    $resultQuantityIsEnough = $pdoAssigned->query("call sp_getStockQuantityCount($productCode,$quantity)");

                    if( $resultQuantityIsEnough->fetchColumn() >0) //the course has been assigned
                            $QuantityIsEnough = true;
                    return $QuantityIsEnough;

                catch (Exception $e) {
                throw $e;   
                }//End For statement

            //Check also if Quantity is enough for that
            if (CheckIfQuantityIsEnough ($productCode,$quantity) == false)
                    $Feedback = "Quantity not enough for this product";


        CREATE PROCEDURE `sp_getStockQuantityCount`(`vStockTypeID` INT, `vQuantity` DECIMAL(9,0))
        declare vQuantityNow int;
        declare vQuantitySubtract int;

        set vQuantityNow=(select Quantity from stockquantitytbl where StockTypeID=vStockTypeID limit 1);
        set vQuantitySubtract=(vQuantityNow-vQuantity);

        select count(Quantity) 
        from stockquantitytbl
        where StockTypeID=vStockTypeID and vQuantitySubtract>=0;



I have done away with the function and now just need to use the Stored Procedure as below:

        //Array data
        //$productCode = array(2, 1, 7, 2);
        //$quantity    = array(10, 20, 30, 40);

        //Single set

        $sql="call sp_getStockQuantityCount($productCode,$quantity)";
        $result = $odb -> query($sql);
        if($result->rowcount() > 0) {
            foreach ($result as $row) {

        $QuantityIsEnough = $row['Quantity'];

        if($QuantityIsEnough == 0)
                    $Feedback = "Quantity not enough for this product";

                    //Insert sale record

One set works well, but the problem still remains on array data as explained above.

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *