# PHP connect to SQL?

**URL:** https://forum.kirupa.com/t/php-connect-to-sql/113405
**Category:** Uncategorized
**Created:** [June 17, 2004, 4:06pm UTC](https://forum.kirupa.com/t/php-connect-to-sql/113405 "2004-06-17T16:06:02Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![guru212](https://avatars.discourse-cdn.com/v4/letter/g/e9c0ed/32.png) [@guru212](https://forum.kirupa.com/u/guru212)
#### Post date: [June 17, 2004, 4:06pm UTC](https://forum.kirupa.com/t/php-connect-to-sql/113405/1 "2004-06-17T16:06:02Z")

</div>

Hey guys,

My firm has just finished a site and we are now working on the admin portion of an area of the site. Having initially been told that the site was going to be hosted on a WIN 2003 server running IIS/SQL – which is our area of proficiency – we estimated the project accordingly.

Now the client has told me that the WIN 2003 is the EVENTUAL destination but that they would like the admin and site to run and reside on their current server, which is a Netscape server that would run PHP but still connect to a SQL database, not MySQL.

I didn’t even know that it was possible to connect PHP to SQL. Do you guys have any experience in this? How would I do it?

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [June 17, 2004, 5:20pm UTC](https://forum.kirupa.com/t/php-connect-to-sql/113405/2 "2004-06-17T17:20:25Z")

</div>

sure you can, and your probably better off using PHP anyway.

this is a quick example of a database connection include file

```php

<?
$hostname = "localhost";
$username = "username";
$password = "password";
$database = "databasename";

$linkID = mysql_connect($hostname, $username, $password);
mysql_select_db($database, $linkID);
?>

```

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [June 17, 2004, 5:40pm UTC](https://forum.kirupa.com/t/php-connect-to-sql/113405/3 "2004-06-17T17:40:38Z")

</div>

Thanks for the reply!

So I should make this a separate file or include this code into the files of my admin?

> [@Digitalosophy](#):
>
> sure you can, and your probably better off using PHP anyway.
> 
> this is a quick example of a database connection include file
> 
> ```php
> 
> <?
> $hostname = "localhost";
> $username = "username";
> $password = "password";
> $database = "databasename";
> 
> $linkID = mysql_connect($hostname, $username, $password);
> mysql_select_db($database, $linkID);
> ?>
> 
> ```

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [June 17, 2004, 6:07pm UTC](https://forum.kirupa.com/t/php-connect-to-sql/113405/4 "2004-06-17T18:07:31Z")

</div>

Just to clarify, when you say ‘SQL’ you mean ‘Microsoft SQL server’, right? SQL is a language, so you can’t connect to it 😉

There are a bunch of PHP functions to access MSSQL. They all start with mssql\_ and look a lot like the MySQL functions. Start here: [http://dk2.php.net/function.mssql-connect](http://dk2.php.net/function.mssql-connect)

Maybe this article is useful as well: [http://www.phpbuilder.com/columns/alberto20000919.php3](http://www.phpbuilder.com/columns/alberto20000919.php3)

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [June 17, 2004, 7:54pm UTC](https://forum.kirupa.com/t/php-connect-to-sql/113405/5 "2004-06-17T19:54:01Z")

</div>

o man i’m sorry i didn’t see you said SQL, i thaught you meant mySQL…

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [June 17, 2004, 10:08pm UTC](https://forum.kirupa.com/t/php-connect-to-sql/113405/6 "2004-06-17T22:08:38Z")

</div>

I have heard about issues with php -\> mssql connections, so you may have to go the ODBC route, which is pretty simple. Just create your database, create a ODBC connector (which on XP and 2k is in the control panel -\> Admin Tools -\> ODBC connector - should be the same for the 2k3 server) which will give you all the ODBC connector strings.

Some info: [http://ca2.php.net/manual/en/function.odbc-connect.php](http://ca2.php.net/manual/en/function.odbc-connect.php)

So if you have trouble connecting directly, try using odbc.
