Jump to content

PHP + MSSQL Database connection

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
Keith

Keith

    Newbie

  • Members
  • Pip
  • 9 posts
I've created a database on SQL Server 2008 and I'm trying to create a connection with PHP, but so far no success. There's also a few things that I don't understand.


<?php

$myServer = "localhost";

$myUser = $DB_User;

$myPass = $DB_Pass;

$myDB = "WebJournal"; // name of both the file and the DB


//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");

  

//close the connection

mssql_close($dbhandle);

?>


- What are supposed to be username and password? A combination that we create here in this code, or are they from the database file? Because, I can't create a password, nor define a username for it on MSSQL Server Studio.

Is it possible that I can't connect due to restrictions on the MSSQL SS application? I tried to move the database from another folder but I only succeeded when I detached. And only could send it to the localhost when I put it offline.



As you can see, I'm a complete noob with PHP; I've read some tutorials, but I can't find anywhere the reason for this problem.

Thanks to anyone that can help me

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
What authentication mode are you using for SQL Server? If it's using Windows authentication, you'll have more difficulty connecting.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Keith

Keith

    Newbie

  • Members
  • Pip
  • 9 posts
Then how can I create a profile to connect via SQL Server Authentication?

#4
so1i

so1i

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 312 posts
Your PHP code is correct. It must be the variable data.

I've never used MSSQL Server, but it seems as WingedPanther says, it's a little more difficult if it is using Windows Auth. Check here.

I always use MySQL to be honest, never had any problems using that. Good luck mate, I'm sure someone here will be able to help you further.

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
You can connect using windows authentication and then create a user. You will have to set preferences, etc.

SQL Server assumes you have a user (username/password) and that the user is granted rights to access one or more databases that are hosted by it. The default administrator username is "sa", with whatever password you created.

If you wanted to connect to the northwind database, it would be:
<?php
$myServer = "localhost";
$myUser = "sa";
$myPass = "MySA_password!1";
$myDB = "northwind"; // name of both the file and the DB

//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");
  
//close the connection
mssql_close($dbhandle);
?> 

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#6
Keith

Keith

    Newbie

  • Members
  • Pip
  • 9 posts
I got it working.
A friend of mine helped setting out SQL Server authentication and the database connection.

But now comes another problem: character encoding.
I'm using "special" characters on my page, since they're an important part of my country's language.
As an example, I'm loading a menu, and the options come straight out of the DB, but since they use characters like á,ã,â,à, etc, they aren't correctly encoded, being displayed with a "�" (question mark) character. My guess is it needs UTF-8 encoding.
Is there any function that can correct this?
For example on this lines:

while($row = mssql_fetch_array($query))
{
print("<ul class=\"col\"><li><a href=\"#\">".$row["Seccao"]."</a></li></ul>");
}


Thanks

#7
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
try these two functions:
PHP: utf8_encode
PHP: utf8_decode
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#8
Keith

Keith

    Newbie

  • Members
  • Pip
  • 9 posts
The utf8_encode function did it.
Thanks everyone!

#9
so1i

so1i

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 312 posts
Glad you got it working mate! :)