Question:
how can i connect through php to a mssql databse on a 2008 server?
2011-11-16 07:11:04 UTC
I need to establish a connection to my server but i keep getting:
Fatal error: Call to undefined function sqlsrv_connect()
Fatal error: Call to undefined function mssql_connect()
what problem am i having?

im using the code

$serverName = "192.168.1.10\DSS"; //serverName\instanceName
$connectionInfo = array( "Database"=>"AutoLogic", "UID"=>"sa", "PWD"=>"");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {
echo "Connection established.
";
}else{
echo "Connection could not be established.
";
die( print_r( sqlsrv_errors(), true));
}?>

which i found online and also
$myServer = "192.168.1.10\DSS";
$myUser = "sa";
$myPass = "";
$myDB = "AutoLogic";

$s = mssql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");

$d = mssql_select_db($myDB, $s)
or die("Couldn't open database $myDB");
?>

which i also found online.


Thank you in advanced!
Three answers:
MV
2011-11-16 19:34:50 UTC
Hi. I guess you are using PHP 5.3, because the MSSQL extension was deprecated since PHP 5.3. So you have two options, depending on what server is running PHP: 1) Windows, install the SQLSRV extension from Microsoft, 2) Linux/Unix, install FreeTDS plus the MSSQL extension. I have used both and they work fine but require a bit of work.



The SQLSRV extension is the way to go if you are running PHP in a Windows server. You need to install the SQLSRV extension (called "sqlsrv driver") and install the SQL Server Native Cliente. Check the page: http://sqlsrvphp.codeplex.com/ for more information and download.



The FreeTDS MSSQL extension can be used both in Windows and Linux (I've used both, too). In Windows you need to install a DLL (php_dblib.dll, it is FreeTDS) in the PHP extension directory and enable it in the php.ini file. (You can download it from the following page: http://docs.moodle.org/20/en/Installing_MSSQL_for_PHP). In Linux you need to install the FreeTDS library, then install the mssql extension (if precompiled by your Linux distribution) or compile it yourself using PHP sources.



Please check the page:

http://docs.moodle.org/20/en/Installing_MSSQL_for_PHP

for some detailed information about using FreeTDS in Windows and Linux. The page is about installing Moodle (a LMS written in PHP) but the configuration information is valid for any server.
raina_vissora
2011-11-16 07:29:46 UTC
Those functions aren't native PHP functions. They're part of the Microsoft SQL Server Driver for PHP, which must be installed in addition to PHP if you want to use them.



It's Windows-exclusive, by the way. So any server you're using it with will need to be running some Windows OS. You only mention your database server as being Windows 2008. If the server you're running PHP on is a different OS, you're going to have some problems.



All relevant information for installing the driver is here:

http://www.php.net/manual/en/book.sqlsrv.php
?
2016-11-19 10:25:41 UTC
via far the least confusing way that i've got got here upon to connect with a sq. server is to course it by using a ODBC connection. a million. Administrative kit -> documents supplies (ODBC) 2. device DNS 3. upload... "sq. Server" Then in basic terms use odbc_connect("sql_test", "consumer", "bypass") rather.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...