我試圖定義一個function
來執行Oracle查詢並動態地使用接收到的string
變量將模式設置爲oci_fetch_array()
。這是函數:PHP警告:oci_fetch_array()期望參數2很長,給出的字符串爲
public function db_single_select($conn,$select,$table,$condition,$fetch_mods='') {
//A string should be sent to "$fetch_mods" with this format: mode1+mode2+etc...
//If a value is not passed to '$fetch_mods', it will be the default fetch mode
$sql = oci_parse($conn, "select $select from $table where $condition");
$sql_result = oci_execute($sql, OCI_DEFAULT);
if($sql_result){
if(empty($fetch_mods)) {
$res = oci_fetch_array($sql);
}
else{
$res = oci_fetch_array($sql, $fetch_mods);
}
}
else{
$res = FALSE;
}
oci_free_statement($sql);
return $res;
}
我這樣調用該函數:
db_single_select($conn, $select, $table_name, $condition, 'OCI_ASSOC');
我得到這樣的警告:
Warning: oci_fetch_array() expects parameter 2 to be long, string given in db_connect.php on line 61
我知道,第二個參數(模式)對於oci_fetch_array()
應該是數字的,因爲它在PHP文檔中是這樣說的。 http://www.php.net/manual/en/function.oci-fetch-array.php
問題是如何根據函數接收的變量設置模式?
既然你可以通過與像
$row = oci_fetch_array ($stid, OCI_ASSOC+OCI_RETURN_NULLS);
一個+
標誌他們分隔條件多種模式,有一個簡單的方法,包括接收OCI_ASSOC+OCI_RETURN_NULLS
一個字符串中的功能,並設置與模式?
'OCI_ASSOC'等是常數;使用它們不帶引號,即'db_single_select($ conn,$ select,$ table_name,$ condition,OCI_ASSOC + OCI_RETURN_NULLS)' – Phil
沒錯!謝謝 – Masoud