views:

45

answers:

3

I am currently tasked with porting our website code onto a linux server (from Win2003). The websites run on a MS SQL database. The original dev created a wrapper to contain all interaction with the database including selecting the correct database (the application spans multiple databases) The problem code is as follows...

if (strcmp($this->_DB_Connection, $param_Database) <> 0) {
  $this->_DB_Selected = @mssql_select_db($param_Database, $this->_DB_Connection);
}

Where $this->_DB_Connection is the connection to the DB server and $param_Database is the name of the database required for the next query.

The problem is that on Windows, strcmp returns 1, on linux it returns NULL.

Anyone have a work around for this that doesn't involve selecting the database each time.

+1  A: 

Either your connection is failing and _DB_Connection is null, or the $param_Database variable is null.

Andomar
neither. _DB_Connection holds a valid resource identifier and $param_Database holds a string
michael
A: 

Is that the actual, "real" code you've copy&pasted or did you type it here?

The second parameter to mssql_select_db() is the database link, it's a resource. I.e. $this->_DB_Connection is either a resource or @mssql_select_db($param_Database, $this->_DB_Connection); will always fail.
If $this->_DB_Connection is a resource then you pass a resource to strcmp($this->_DB_Connection, $param_Database) and the implicit conversion to string will be something like resource #3. So, unless your database is called resource #3 your strcmp() will always fail.

Make your choice which of the two functions will always fail with the code you've provided.

To make this approach work store the name of the currently selected database in a property of the object and use it for comparison.

  protected $_Current_Database = null;

  [...]  

    if ( 0!==strcmp($this->_Current_Database, $param_Database) ) {
      $this->_DB_Selected = @mssql_select_db($param_Database, $this->_DB_Connection);
      if ( $this->_DB_Selected ) {
        $this->_Current_Database = $param_Database;
      }
      else { // select_db failed.
        $msg = mssql_get_last_message();
        // do something with $msg here, e.g. echo 'mssql message: ', $msg;
      }
    }  
  [...]
VolkerK
+1 Much better answer
Andomar
Yes this is the actual code (please nb. that I did not write this). $this->_DB_Connection is a valid resource identifier. mssql_select_db works if I remove the if statement. Agreed I would have expected the strcmp to fail (as it does under the linux host) but it passes in Windows.I dont like this code, binary comparing a resource to a string is bad, hence I'm asking for a better solution
michael
@michael: To believe that this code works on a windows machine requires more faith in magic than I have ;-) Exactly how did you test it? With a debugger like xdebug? Some var_dump/echo lines?
VolkerK
I don't really need you to believe it works, that's not the point of this question, I need an alternative.
michael
Install a debugger like xdebug, set a breakpoint on the strcmp() line and see for yourself.
VolkerK
A: 

I've ended up just storing the name of the last selected database in a member variable of the DB wrapper object and am comparing that instead of the SQL connection

michael