繁体   English   中英

传递给的参数1必须是int的实例,给定整数

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

我刚刚开始学习php,并按照http://www.mysqltutorial.org/php-calling-mysql-stored-procedures/中的示例进行操作。

但是当我试图运行时得到错误“传递给参数1必须是int的实例,给定的整数...”:

   <?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));

在我看来,作为int的IN参数的定义已经完成。 你能给我一些提示如何使这项工作?

仅从PHP 7.0.0支持标量类型的类型提示/声明(bool,int,float,string)。

请参阅PHP手册/函数参数以及类似问题的答案

无需在PHP中输入提示参数。 在某些情况下,您可以使用类型提示(对于类非常有用)。 在您的情况下,PHP需要一个int类的实例,它没有定义。 您可以使用is_int函数检查传递的参数是否为整数

你可以用这个替换param绑定:

  $stmt->bindParam(':id', $customerNumber);

在这种情况下不需要第三个参数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM