简体   繁体   English

php oci_bind_by_name float to numeric

[英]php oci_bind_by_name float to numeric

I need to bind floating point number to OCI statement. 我需要将浮点数绑定到OCI语句。

What I'm doing: 我在做什么:

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

In my Oracle DB 'price' is an argument of the stored procedure and it's type is NUMERIC. 在我的Oracle DB中,'price'是存储过程的参数,它的类型是NUMERIC。

After executing my statement I'm getting the following error: 执行我的语句后,我收到以下错误:

Message: oci_execute() [function.oci-execute]: ORA-06502: PL/SQL: numeric or value error: character to number conversion error ORA-06512: at line 1 消息:oci_execute()[function.oci-execute]:ORA-06502:PL / SQL:数字或值错误:字符到数字转换错误ORA-06512:在第1行

If $price is an integer everything works fine. 如果$ price是一个整数,一切正常。 In PHP docs http://lv.php.net/manual/en/function.oci-bind-by-name.php I haven't found a special type for floats for the fifth parameter (int $type = SQLT_CHR). 在PHP文档中http://lv.php.net/manual/en/function.oci-bind-by-name.php我没有为第五个参数(int $ type = SQLT_CHR)找到浮点数的特殊类型。

Answer found: I just changed decimal symbol in my OS from "," to "." 回答:我刚刚将操作系统中的十进制符号从“,”更改为“。” and now everything works fine 现在一切正常

If you can't change the decimal symbol of your OS (or you simply don't want to), the only solution for this issue is to avoid float parameters. 如果您无法更改操作系统的十进制符号(或者您根本不想这样做),则此问题的唯一解决方案是避免浮点参数。 You must enter the the value directly into the sql. 您必须直接在sql中输入值。 You must also be aware to use en_US as locale for the correct decimal separator. 您还必须知道使用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);
}

Try: oci_bind_by_name($resource, 'price', $price, -1, SQLT_NUM); 尝试: oci_bind_by_name($resource, 'price', $price, -1, SQLT_NUM); SQLT_NUM is just missing from the documentation. 文档中缺少SQLT_NUM。

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

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