Jump to content

Recommended Posts

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 :P

 

Thanks

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

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.

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.