Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Argument 1 passed to must be an instance of int, integer given

Tags:

php

mysql

I am just beginning to learn php, and following an example from http://www.mysqltutorial.org/php-calling-mysql-stored-procedures/.

But got error "Argument 1 passed to must be an instance of int, integer given..." when I tried to run:

   <?php

    require_once 'dbconfig.php';

    /**
     * Get customer level
     * @param int $customerNumber
     * @return string
     */
    function getCustomerLevel(int $customerNumber) {
        try {
            $pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);

            // calling stored procedure command
            $sql = 'CALL GetCustomerLevel(:id,@level)';

            // prepare for execution of the stored procedure
            $stmt = $pdo->prepare($sql);

            // pass value to the command
            $stmt->bindParam(':id', $customerNumber, PDO::PARAM_INT);

            // execute the stored procedure
            $stmt->execute();

            $stmt->closeCursor();

            // execute the second query to get customer's level
            $row = $pdo->query("SELECT @level AS level")->fetch(PDO::FETCH_ASSOC);
            if ($row) {
                return $row !== false ? $row['level'] : null;
            }
        } catch (PDOException $e) {
            die("Error occurred:" . $e->getMessage());
        }
        return null;
    }

    $customerNo = 103;
    echo sprintf('Customer #%d is %s', $customerNo, getCustomerLevel($customerNo));

looks to me the definition of the IN parameter as int is already done. Could you give me some hint how to make this work?

like image 975
Viviannne Avatar asked Oct 19 '16 19:10

Viviannne


2 Answers

Type hinting / declaration for scalar types (bool, int, float, string) is supported from PHP 7.0.0 only.

See PHP manual / Function arguments as well as this answer to similar question

like image 187
user2038893 Avatar answered Sep 28 '22 06:09

user2038893


No need to typehint parameters in PHP. You can use typehinting in some cases though (it is very usefull with classes). In your case, PHP expects an instance of the int class, which is not defined. You can use the is_int function to check if the parameter passed is an integer

like image 25
Devin Avatar answered Sep 28 '22 06:09

Devin