繁体   English   中英

php oci_bind_by_name float to numeric

[英]php oci_bind_by_name float to numeric

我需要将浮点数绑定到OCI语句。

我在做什么:

$price = 0.1
oci_bind_by_name($resource, 'price', $price);

在我的Oracle DB中,'price'是存储过程的参数,它的类型是NUMERIC。

执行我的语句后,我收到以下错误:

消息:oci_execute()[function.oci-execute]:ORA-06502:PL / SQL:数字或值错误:字符到数字转换错误ORA-06512:在第1行

如果$ price是一个整数,一切正常。 在PHP文档中http://lv.php.net/manual/en/function.oci-bind-by-name.php我没有为第五个参数(int $ type = SQLT_CHR)找到浮点数的特殊类型。

回答:我刚刚将操作系统中的十进制符号从“,”更改为“。” 现在一切正常

如果您无法更改操作系统的十进制符号(或者您根本不想这样做),则此问题的唯一解决方案是避免浮点参数。 您必须直接在sql中输入值。 您还必须知道使用en_US作为正确小数分隔符的区域设置。

// Ensure that the period is used as decimal separator when converting float to string
setlocale(LC_ALL, 'en_US');

// Generate SQL
// ...
$variables = array();
if(is_int($myValue))
{
    $sql .= ':MYVALUE';
    $variables[':MYVALUE'] = $myValue;
}
else if(is_float($myValue))
{
    $sql .= (string) $myValue;
}
// ...

// Generate statement
// $resource = oci_parse(...);

// Bind parameters (if neccessary)
if(count($variables) > 0)
{
    foreach($variables as $name => &$variable)
        oci_bind_by_name($resource, $name, $variable);
}

尝试: oci_bind_by_name($resource, 'price', $price, -1, SQLT_NUM); 文档中缺少SQLT_NUM。

暂无
暂无

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

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