I am currently tasked with porting our website code onto a linux server (from Win2003). The websites run on a SQL Server 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.
Either your connection is failing and _DB_Connection
is null, or the $param_Database
variable is null.
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
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;
}
}
[...]
精彩评论