Creole/Propel/PHP MSSQL connection problem

Posted on 2006-04-26
Last Modified: 2013-12-12
Hi Everybody! I hope someone can help me.


I'm working on a php-propel-creole framework. I was using it with mysql with no problem at all. But now I need to change to mssql and when I try to connect to my bd in order to use the Peer objects I get the following error:



Fatal error: Uncaught exception 'SQLException' with message 'connect failed [Native Error: Database context changed to 'master'.] [User Info: Array]' in c:\apachefriends\xampp\php\pear\creole\drivers\mssql\MSSQLConnection.php:94 Stack trace: #0 c:\apachefriends\xampp\php\pear\creole\drivers\mssql\MSSQLConnection.php(94): MSSQLConnection::connect() #1 C:\apachefriends\xampp\php\pear\creole\Creole.php(243): MSSQLConnection->connect(Array, 0) #2 C:\apachefriends\xampp\htdocs\esic\codeBehind\esic.php(30): Creole::getConnection(Array) #3 C:\apachefriends\xampp\htdocs\esic\inc\main.php(46): include('C:\apachefriend...') #4 C:\apachefriends\xampp\htdocs\esic\index.php(28): include('C:\apachefriend...') #5 {main} thrown in c:\apachefriends\xampp\php\pear\creole\drivers\mssql\MSSQLConnection.php on line 94


The database I want to connect to is named registro_civil and the server is REGISTROCIVIL (it's the name of the computer). I configure the dsn as:


$dsn = array('phptype' => 'mssql',

             'hostspec' => 'REGISTROCIVIL',

             'username' => 'sa',

             'password' => '',

             'database' => 'registro_civil');


Is there any problem with this????


Any help will be appreciated. Thanks in advance.

Question by:bethzycb
    LVL 40

    Accepted Solution

    Some links ...

    I suspect that the warning generates an error number. Your code will need to ignore this error number.

    I don't use Creole, so I can't say for sure what the code should do.

    Can you provide creole\drivers\mssql\MSSQLConnection.php - ideally the function containing line 94.

    Maybe there is already some code to assist in filtering error conditions.

    Author Comment

    This is the function on creole\drivers\mssql\MSSQLConnection.php where the error is generated:

     function connect($dsninfo, $flags = 0)
            if (!extension_loaded('mssql') && !extension_loaded('sybase') && !extension_loaded('sybase_ct')) {
                throw new SQLException('mssql extension not loaded');

            $this->dsn = $dsninfo;
            $this->flags = $flags;
            $persistent = ($flags & Creole::PERSISTENT === Creole::PERSISTENT);

            $user = $dsninfo['username'];
            $pw = $dsninfo['password'];
            $dbhost = $dsninfo['hostspec'] ? $dsninfo['hostspec'] : 'localhost';
            if (PHP_OS == "WINNT" || PHP_OS == "WIN32") {
                $portDelimiter = ",";
            } else {
                $portDelimiter = ":";
            if(!empty($dsninfo['port'])) {
                    $dbhost .= $portDelimiter.$dsninfo['port'];
            } else {
                    $dbhost .= $portDelimiter.'1433';
            $connect_function = $persistent ? 'mssql_pconnect' : 'mssql_connect';

            if ($dbhost && $user && $pw) {
                $conn = @$connect_function($dbhost, $user, $pw);
            } elseif ($dbhost && $user) {
                $conn = @$connect_function($dbhost, $user);
            } else {
                $conn = @$connect_function($dbhost);
            if (!$conn) {
    line 94----->            throw new SQLException('connect failed', mssql_get_last_message());
            if ($dsninfo['database']) {
                if (!@mssql_select_db($dsninfo['database'], $conn)) {
                    throw new SQLException('No database selected');              
                $this->database = $dsninfo['database'];
            $this->dblink = $conn;        

    Here, there is no try/catch code because from here the SQLException is thrown. Furthermore, it's the connection to the server which is not possible, and therefore it can't select ANY database at all, not even the "master" one. So I GUESS the error is the failure in the connection not the context changed issue.

    I tried catching the error on the parts of code that call this MSSQLConnection.php script, but if I do so, I get no database selected.

    The only thing I GUESS could be wrong might be the parameters the connect_function (mssql_connect) is receiving and those are: "server=REGISTROCIVIL,1433  user=sa password=". I don't know if the sintax SERVER,port (REGISTROCIVIL,1433) or the port number is being sent is correct.

    Does anybody have any idea what's wrong?
    LVL 40

    Expert Comment

    You will need the password for the connection. Unless you are using trusted connections, you may not get access.

    The port number is OK and the default value.

    The three @ signs in the lines above the exception, can you remove them. They are suppressing the error and allowing the exception to deal with it.


    Author Comment

    Thanks RQuadling, can you tell me, how do I know if I'm using trusted connections?
    LVL 40

    Expert Comment

    Your SQL server admin will tell you if trusted connections are allowed.

    In those cases, you normally don't supply a user name or a password.

    Author Comment

    Ehm... sorry but I'm newbie in sql server administration. I tried modifying security of my sqlserver and allowing windows nt ONLY authentication. I don't know if that allow me trusted connections :$ But after doing so, things keep the same, the connect fail is still there :(

    Any idea?

    Author Comment

    After some workaround, I could make the server get connected.

    What I did was:
    - I changed the ntwdblib.dll of c:\windows\system32 to the one of version 2000.80.194.0 and deleted the one in c:\php

    - I enabled trusted connections, modifying security in my sqlserver allowing Windows NT ONLY Authentication.

    - I removed the PORT number from the dsn string used to get connected

    And after all of this I could get connected.

    NOTE: I didn't need to catch any error on the try/catch code.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now