5

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

我在做什么:

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

在我的 Oracle DB 中,“价格”是存储过程的一个参数,它的类型是 NUMERIC。

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

消息:oci_execute() [function.oci-execute]: ORA-06502: PL/SQL: numeric or value error: character to number conversion error ORA-06512: at line 1

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

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

4

2 回答 2

0

如果您无法更改操作系统的十进制符号(或者您根本不想更改),则此问题的唯一解决方案是避免使用浮点参数。您必须将值直接输入到 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);
}
于 2017-08-09T14:09:05.710 回答
-1

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

于 2015-06-24T12:48:49.513 回答