2011-03-26 71 views
1

mysql擴展函數正在接受可選鏈接參數,如果未指定,它們將使用mysql_connect調用中的最後一個返回值。我想在我的高級函數中使用相同的行爲,但不知道如何實現它。下面是例子醜陋的我不想使用,絕對:正確處理mysql_XXXX默認鏈接標識符參數

function get_mysql_info($linkid = null){ 
    $linkid ? $strInfo = mysql_info($linkid) : $strInfo = mysql_info(); 
    // followed by multiple tortures by ereg calls even! 

我看到的可能(也可能不會)工作兩種可能的方式:

  1. 中繼一些真正的空值(NULL不明確限定,功能都在抱怨),以較低的水平,使MySQL功能,認爲這是沒有聯繫的說法,並做最後使用想起資源
  2. 檢索最後記得的資源去默認方式

如果還沒有成功,請告知,謝謝!


藉口重複,這是僞代碼試圖進一步解釋什麼,我想:

function mysql_wrap ($optional_link_identifier) { 
    mysql_this($optional_link_identifier); // use specified link or default (as described above) 
    mysql_that($mandatory_parameter, $optional_link_identifier); // use specified link or default 
    // so on 
} 

...而不是分支上存在/不存在可選參數的:

function mysql_wrap ($optional_link_identifier = NULL) { 
    if ($optional_link_identifier) 
    mysql_this($optional_link_identifier); // use specified link 
    else mysql_this(); // or default 
    if ($optional_link_identifier) 
    mysql_that($mandatory_parameter, $optional_link_identifier); // use specified link 
    else mysql_that(); // or default 
    // so on. as you see, generalizing the code in the function actually made things even worse 
} 

所以,我正在尋求通用的方式來處理這兩種情況。

+2

爲什麼你絕對不想用最自然的方式來表達這個?你能說一點嗎? – Jon 2011-03-26 15:37:13

回答

-1

mysql_ *函數有他們的一天。

使用PDO,您將獲得一個完全的OOP接口,它也很大程度上獨立於正在使用的數據庫服務器的類型。

+0

「用X代替Y」的答案毫無用處。 – 2011-04-07 23:02:19

+0

發佈的問題與隱式使用由mysql_connect創建的句柄具體相關。使用OO DB層(如PDO)完全消除了這一要求,因爲所有調用都是特定於實例的。 – Stephen 2011-04-08 00:53:01

+0

是的,使用TomCat甚至不需要訴諸PHP5的可疑OOP。原**特定**問題仍然存在。 – 2011-04-08 01:01:41