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?
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With