我问,因为我有一个布尔值传递给PDO的问题。PHP的is_bool如何区分INT和BOOL?
这个功能让我真/假:
public function getBoolVal($var){
if (!is_string($var)) return (bool) $var;
switch (strtolower($var)) {
case '1':
case 'true':
case 'on':
case 'yes':
case 'y':
return true;
default:
return false;
}
}
我有这样一行数据绑定到参数的函数中:
$this->db->bind('active', (bool)$this->getBoolVal($pa['active']));
和bind
函数内部是:
foreach ($this->parameters as $param => $value) {
$type = PDO::PARAM_STR;
switch ($value[1]) {
case is_int($value[1]):
$type = PDO::PARAM_INT;
break;
case is_bool($value[1]):
$type = PDO::PARAM_BOOL;
break;
case is_null($value[1]):
$type = PDO::PARAM_NULL;
break;
}
// Add type when binding the values to the column
$this->sQuery->bindValue($value[0], $value[1], $type);
}
当这些值被绑定时,我得到PDO::PARAM_BOOL
as类型当输入值$pa['active']
是1,但我总是PDO::PARAM_INT
如果输入为0 ...
所以给予1或0,如何在地球上不会is_bool
知道我想要什么???或者我该如何解决这个问题?
自发布此,我和我的数据库中的字段TINYINT免掉。我认为使用整数与我正在处理的外部数据“更安全”。 - 很高兴被告知,否则,虽然:) – Warren
你为什么不把所有的价值作为字符串? –