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:

<?php
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!

Thanks!!

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 (http://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx)

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.

Thanks!

These might be of help:
http://social.technet.microsoft.com/wiki/contents/articles/accessing-sql-server-databases-from-php.aspx#Using_the_SQLSRV_Driver
http://social.technet.microsoft.com/wiki/contents/articles/accessing-sql-server-databases-from-php.aspx#Using_the_PDO_Driver

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:

<?php
$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);


mysql_query("INSERT INTO BRANCH (DIV, DIVNAME, CITY)
VALUES ('60', 'ADMIN', DUNDEE')");
mysql_close($con);
?>

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.

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

@deucalion0
Here is php.net documentation that has examples. My suggestion it to start reading through them to get a better understanding of how the driver is used.
http://us3.php.net/manual/en/book.sqlsrv.php

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.