开发者

Handle mysql_XXXX default link identifier argument gracefully

mysql extension functions are accepting optional link argument, and if not specified, they use last return value from mysql_connect call. I want the same behaviour in my higher level function(s), but have no idea how to implement it. Below is example of ugliness i DO NOT want to use, absolutely:

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

I see two possible ways which might (or might not) work:

  1. passthru some real void value (NULL does not qualify, functions are complaining) to lower level and make mysql function to think there is no link argument and do use last remembered resource
  2. retrieve that last remembered resource and go default way explicitly

Had no success with either variant yet, please advise, and thank you!


Excuse the repeat, this is pseudocode attempt to explain further what i want:

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
}

... as opposed to branching on presence/absence of the optional argument:

function mysql_wrap ( $optional_link_identi开发者_如何转开发fier = 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
}

so, i'm seeking the way to handle both cases universally.


the mysql_* functions have had their day.

Use PDO, and you get a fully OOP inteface that is also largely independent of the type of DB server being used.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜