簡體   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