Adkar Posted February 24, 2011 Share Posted February 24, 2011 Hi guys, I have a problem. been looking at forums all day but cant find the right one to help me out. I'm using Windows 7 and EW(Expression Web) 4 to create a php site, but have had trouble all day trying to get it to connect to a MSSQL database (SQL Server 2008 R2). I'm a complete noob with php so please bare with me Here is my code to connect to the database <? $myServer = ".\MSSQLHOSTING"; $myUser = "Username"; $myPass = "Password"; $myDB = "database"; //connection to the database $dbhandle = mssql_connect($myServer, $myUser, $myPass) or die("Couldn't connect to SQL Server on $myServer"); //select a database to work with $selected = mssql_select_db($myDB, $dbhandle) or die("Couldn't open database $myDB"); //declare the SQL statement that will query the database $query = "SELECT UserName, UserPassword"; $query .= "FROM Administrator"; $query .= "WHERE UserName='Administrator'"; //execute the SQL query and return records $result = mssql_query($query); $numRows = mssql_num_rows($result); echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>"; //display the results while($row = mssql_fetch_array($result)) { echo "<li>" . $row["UserName"] . $row["UserPassword"]. "</li>"; } //close the connection mssql_close($dbhandle); ?> I get this error from this code: Fatal error: Call to undefined function mssql_connect() What files and code do i need for this to work? Any help is welcome but remember i'm new to php Thanks Quote Link to comment https://forums.phpfreaks.com/topic/228699-php-connection-to-mssql-database-in-windows/ Share on other sites More sharing options...
girtskazus Posted February 24, 2011 Share Posted February 24, 2011 Easier way is not this? <?php mysql_connect(".\MSSQLHOSTING", "Username", "Password") or die("Connection Failed"); mysql_select_db("database")or die("Connection Failed"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/228699-php-connection-to-mssql-database-in-windows/#findComment-1179093 Share on other sites More sharing options...
Brian Swan Posted February 24, 2011 Share Posted February 24, 2011 The error you are getting indicates that the php_mssql extension is not loaded. You can verify this by calling phpinfo() - my guess is that you won't see mssql as a loaded extension. Since you are running on Windows, I'd suggest that you use the MSFT-supported extension: php_sqlsrv. You can read about the differences between those two extensions here: http://blogs.msdn.com/b/brian_swan/archive/2010/03/08/mssql-vs-sqlsrv-what-s-the-difference-part-1.aspx. You can download the sqlsrv extension here: http://www.microsoft.com/downloads/en/details.aspx?FamilyID=80e44913-24b4-4113-8807-caae6cf2ca05. Regardless of which driver you use, you need to put the extension file in your PHP extension directory, add the appropriate entry to your php.ini file, and restart your web server. Hope that helps. -Brian Quote Link to comment https://forums.phpfreaks.com/topic/228699-php-connection-to-mssql-database-in-windows/#findComment-1179119 Share on other sites More sharing options...
Pikachu2000 Posted February 25, 2011 Share Posted February 25, 2011 Easier way is not this? <?php mysql_connect(".\MSSQLHOSTING", "Username", "Password") or die("Connection Failed"); mysql_select_db("database")or die("Connection Failed"); ?> The OP is using MS SQL, not MySQL. Quote Link to comment https://forums.phpfreaks.com/topic/228699-php-connection-to-mssql-database-in-windows/#findComment-1179339 Share on other sites More sharing options...
Adkar Posted February 25, 2011 Author Share Posted February 25, 2011 Thanks for the replies guys, @ Brian i downloaded the sqlsrv extension and installed it to C:\php\ext i dont have a php.ini file though. where could i get this file from? once i have modified the php.ini file with the extension mentioned in your link, and my code to sqlsrv instead of mssql would my code work? Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/228699-php-connection-to-mssql-database-in-windows/#findComment-1179447 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.