mssql connection works procedurally but not with database class

#1
[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'];
        }

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

#3
[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

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

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

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

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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.