【问题标题】:php oci_bind_by_name float to numericphp oci_bind_by_name 浮动到数字
【发布时间】:2012-02-10 09:01:44
【问题描述】:
我需要将浮点数绑定到 OCI 语句。
我在做什么:
$price = 0.1
oci_bind_by_name($resource, 'price', $price);
在我的 Oracle 数据库中,“价格”是存储过程的一个参数,它的类型是 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) 的特殊浮点类型。
找到的答案:
我刚刚将操作系统中的十进制符号从“,”更改为“。”现在一切正常
【问题讨论】:
标签:
php
oracle
data-binding
oracle-call-interface
【解决方案1】:
尝试:
oci_bind_by_name($resource, 'price', $price, -1, SQLT_NUM); SQLT_NUM 只是在文档中丢失。
【解决方案2】:
如果您无法更改操作系统的小数点符号(或者您根本不想),则解决此问题的唯一方法是避免使用浮点参数。
您必须将值直接输入到 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);
}