So I have Microsoft SQL Server 2008 R2 installed and an instance set up with a database an user setup on that database with a password. I have installed PHP, and IIS and got PH working from the WWWROOT folder, but when tryng to connect to my database it is not just not working. I have spent two days straight on this, and pulling my hair out. I was able to connect to the database using ColdFusion and its web server, PHP was meant to be easier! Here is the code i am using to connect to my database:

mysql_connect("MYINSTANCE", "MYUSER", "MYPASSWORD") or die(mysql_error());
echo "Connected to MySQL<br />";
mysql_select_db("test") or die(mysql_error());
echo "Connected to Database";

Obviously I have omitted my server, user and password from the above code. I have installed the drivers from MS and installed into the PHP EXT folder, still no luck, I will appreciate any help from you guys!


Recommended Answers

All 8 Replies

MySQL is not the same as MSSQL. You should not be using the mysql* functions to connect to a microsoft sql server.You want to be using the SQL Server driver (

Hi there many thanks for your reply. My mistake, i am using Microsoft SQL server 2008 R2 and not MySQL, I always get confused! ARRGH! The driver you pointed out is in fact the one i downloaded and installed to my PHP/ext directory, I don't know if there was more configuration to do after this, as the tutorial i was following didn't say.


These might be of help:

Wow that actually helped a lot, I eventually got it to connect! YES!!!

But bit of an anti climax as I was excited to be able to insert into tables, but I cannot get this to work, have you any ideas? I am using this code:

$serverName = "myinstance";
$connectionInfo = array("UID" => "myuser", "PWD" => "mypassword", "Database"=>"mydatabase");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn === false ) // note the format of ‘equals’
     echo "Could not connect.\n";
     die( print_r( sqlsrv_errors(), true));

mysql_select_db("TutorialTwo", $con);

VALUES ('60', 'ADMIN', DUNDEE')");

I get no errors, but when I refresh the table in ms sql there is no new addition, I wonder what i am doing wrong??

Thanks for all your help!!!!

You're back to using mysql functions. You can't mix them together.

The op is not using the mssql driver, they are using the sqlsrv driver provided my Microsoft for php on windows.

Here is documentation that has examples. My suggestion it to start reading through them to get a better understanding of how the driver is used.

ah, sounds like microsoft, complicate and make things painful.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.