Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

PHP + MSSQL Database connection

mssql connection

  • Please log in to reply
8 replies to this topic

#1 Keith

Keith

    CC Lurker

  • Just Joined
  • Pip
  • 9 posts

Posted 24 November 2009 - 08:24 AM

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
  • 0

#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 24 November 2009 - 08:31 AM

What authentication mode are you using for SQL Server? If it's using Windows authentication, you'll have more difficulty connecting.
  • 0

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

My MineCraft server site: http://banishedwings.enjin.com/


#3 Keith

Keith

    CC Lurker

  • Just Joined
  • Pip
  • 9 posts

Posted 24 November 2009 - 08:36 AM

Then how can I create a profile to connect via SQL Server Authentication?
  • 0

#4 so1i

so1i

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 297 posts

Posted 24 November 2009 - 11:14 AM

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.
  • 0

#5 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 24 November 2009 - 11:51 AM

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

  • 0

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

My MineCraft server site: http://banishedwings.enjin.com/


#6 Keith

Keith

    CC Lurker

  • Just Joined
  • Pip
  • 9 posts

Posted 25 November 2009 - 04:08 PM

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
  • 0

#7 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 25 November 2009 - 05:01 PM

try these two functions:
PHP: utf8_encode
PHP: utf8_decode
  • 0

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.


#8 Keith

Keith

    CC Lurker

  • Just Joined
  • Pip
  • 9 posts

Posted 26 November 2009 - 01:43 AM

The utf8_encode function did it.
Thanks everyone!
  • 0

#9 so1i

so1i

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 297 posts

Posted 26 November 2009 - 11:19 AM

Glad you got it working mate! :)
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download