超出范围值调整到列类型的最大值。
你的PHP应用在使用PDO连接MySQL时遇到了数值范围错误:
'pdo_code' => '22003',
'db_code' => 1264,
'db_error' => 'Out of range value for column'
function validateColumnRange($value, $columnType, $columnName) {
switch ($columnType) {
case 'TINYINT':
$min = -128;
$max = 127;
break;
case 'TINYINT UNSIGNED':
$min = 0;
$max = 255;
break;
case 'INT':
$min = -2147483648;
$max = 2147483647;
break;
case 'INT UNSIGNED':
$min = 0;
$max = 4294967295;
break;
default:
return true;
}
if ($value < $min || $value > $max) {
throw new InvalidArgumentException(
"列 '{$columnName}' 的值 {$value} 超出范围 [{$min}, {$max}]"
);
}
return true;
}
try {
$age = 300;
validateColumnRange($age, 'TINYINT UNSIGNED', 'age');
$stmt = $pdo->prepare("INSERT INTO users (age) VALUES (?)");
$stmt->execute([$age]);
} catch (InvalidArgumentException $e) {
error_log($e->getMessage());
}