CodeIgniter Forums
mssql connection works procedurally but not with database class - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: mssql connection works procedurally but not with database class (/showthread.php?tid=22743)



mssql connection works procedurally but not with database class - El Forum - 09-18-2009

[eluser]getSurreal[/eluser]
I'm able to successfully connect procedurally to my MS SQL database, but when I try to use the database class I have problems.

The first thing I noticed is that mssql.php is coded incorrectly to use port which was recently added for mssql.
Code:
function db_connect()
    {
        if ($this->port != '')
        {
            $this->hostname .= ','.$this->port;
        }
This code places a comma between hostname and port number, but the proper syntax from php.net documentation is hostname:port

After I updated mssql.php with that change I no longer got the error about being unable to connect to my database. But I'm still getting an error that I can't resolve.

This code fails
Code:
$this->load->database('mssqldb');
        $this->db->query('select * from mytable');
        foreach ($query->result() as $row)
        {
           echo $row->Hosts;
        }

with this error
Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: query

Filename: report/report.php

Line Number: 8

Fatal error: Call to a member function result() on a non-object in /home/vmr/public_html/system/application/controllers/report/report.php on line 8

here are my settings in database.php

Code:
$db['mssqldb']['hostname'] = "myserver";
$db['mssqldb']['username'] = "myuser";
$db['mssqldb']['password'] = "mypass";
$db['mssqldb']['database'] = "mydatabse";
$db['mssqldb']['dbdriver'] = "mssql";
$db['mssqldb']['dbprefix'] = "";
$db['mssqldb']['pconnect'] = TRUE;
$db['mssqldb']['db_debug'] = TRUE;
$db['mssqldb']['cache_on'] = FALSE;
$db['mssqldb']['cachedir'] = "";
$db['mssqldb']['port'] = 931;


Procedurally I can connect and return data with this code.

Code:
$sqlconnect=mssql_connect('myserver:931', 'myuser', 'mypass');
        $sqldb=mssql_select_db('mydatabase',$sqlconnect);
        $sqlquery="SELECT * from mytable;";
        $result= mssql_query($sqlquery);
        while ($row=mssql_fetch_array($result))    {
            echo $row['Hosts'];
        }



mssql connection works procedurally but not with database class - El Forum - 09-25-2009

[eluser]getSurreal[/eluser]
Bump.... I hope someone can help me with this.


mssql connection works procedurally but not with database class - El Forum - 09-25-2009

[eluser]bgreene[/eluser]
well if "$this->db->query('select * from mytable);" is really what you have and not just a mistype then you're missing the closing apostrophe after mytable


mssql connection works procedurally but not with database class - El Forum - 09-25-2009

[eluser]getSurreal[/eluser]
Just a typo. I just double checked my code and corrected the post.


mssql connection works procedurally but not with database class - El Forum - 09-25-2009

[eluser]bgreene[/eluser]
$this->load->database('mssqldb');
$qry = $this->db->query('select * from mytable');
foreach ($qry->result() as $row)
{
echo $row->Hosts;
}


mssql connection works procedurally but not with database class - El Forum - 09-25-2009

[eluser]getSurreal[/eluser]
bgreene, that worked!

Thank you very much. The CI example does not show it that way.