【发布时间】:2017-01-20 19:29:17
【问题描述】:
我刚刚开始学习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));
在我看来 IN 参数的定义为 int 已经完成。你能给我一些提示吗?
【问题讨论】:
-
听起来你正在运行一个
-
所以只需在此处删除
int表单:getCustomerLevel(int $customerNumber) -
如果可以的话,升级到 PHP 7。如果您只是学习,不妨学习最新版本。
-
我会争辩说,除非您在示例代码中定义它(不太可能)或类型转换它,否则无论如何传递时它将是一个字符串(来自
$_POST,DB 等,所以为什么要强制int在这里?