[eluser]Sergio B[/eluser]
Hello
For a couple of days now I have been investigating how to connect to a Microsoft SQL Server database with PHP from Linux. After some searching through the web I got the FreeTDS and the unixODBC set up and now I can use the tsql and isql commands to connect to to the SQL Server and I can also do it from a PHP script and retrieve the data:
Code:
<?php
$link = odbc_connect("SQLTest", "sa", "Password12345");
if(!$link)
{
echo "SQL Server connection failed";
}
odbc_exec($link, "USE TestDB");
$result = odbc_exec($link, "SELECT * FROM TestTable");
echo "<table>";
echo "<tr><th>TestID</th><th>TestString</th></tr>";
while(odbc_fetch_row($result))
{
echo "<tr><td>".odbc_result($result, "TestID")."</td><td>".odbc_result($result, "TestString")."</td></tr>";
}
echo "</table>";
odbc_free_result($result);
odbc_close($link);
?>
Now when it comes to doing it with CodeIgniter I’m clueless. As you can see I have set up a System DSN (SQLTest) to use with the odbc_connect function but I have no idea how to use it with the CodeIgniter Database class. This is the database.php file I tried to use:
Code:
$db['default']['hostname'] = "192.168.100.1";
$db['default']['username'] = "sa";
$db['default']['password'] = "Password12345";
$db['default']['database'] = "TestDB";
$db['default']['dbdriver'] = "odbc";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = FALSE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";
What I got was a nice "Unable to connec to your database server using the provided settings".
I don’t know what the error is or how is CI trying to establish the connection. Any help would be appreciated.
PD: Off course I tried to use the "mssql" setting for "dbdriver" parameter but off course it didn't work which is not strange since there is no php_msssql extension for linux.