Question

Cannot connect to database from php pages

Asked by: edukate

Hi there I've been trying to setup a moodle website on Windows 2008 with IIS7 for several weeks now and it's very difficult, but I'm quite close..
I've posted on the moodle forums but these aren't monitored very much and I'm a bit noob at php http://moodle.org/mod/forum/discuss.php?d=135375
Our problem at the moment (hopefully last one) is with the application connecting to the database. I've tried using both mysql and mssql but I get errors with both.

Is there some simple was I can test what the problem may be?

Can we use another simpler php program to test database connection?

I don't have any freetds.config file or the option for it's DLL in php.ini

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-11-05 at 12:16:28ID24875908
Tags

php

,

mysql

,

freetds

,

iis

Topics

PHP for Windows

,

PHP and Databases

,

MySQL Server

Participating Experts
2
Points
300
Comments
28

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. php & MSSQL
    Myplatform is Linux (Redhat 8.0) + php+mysql+apache, that is normally running. By the way I just intend to use php to connect MSSQL by "mysql_connect", but it failed. So I want to verify something.... 1. No any error come out during php running ? I am not sure...
  2. no php.ini
    I use phpinfo it tell me that php.ini is in /tec but when i go to /etc no php.ini I use commnd find but not found . It mean that i not have php.ini or not Thank
  3. php 5 on win2000 sbs with MSsql and MYsql
    i am trying to run a intranet on our local server. Previuos attempts were made by other parties using MSsql and i am using MYsql. questions: 1) can they both run together. Other party still completing contract so do not want to stop it 2)I have modified th php.ini file a...
  4. PHP load php.ini
    Ok, I've posted this problem before and thought i had figured it out, but i havn't. I'm trying to run PHP from a command prompt and i can run the most basic form of php, but i can't get php to load php.ini. I've put php.ini in all of the default locations including: -current...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: TiberiuGalPosted on 2009-11-05 at 12:25:21ID: 25753566

Hi,
Please give us the error messages you receive - both for MySQL and SQL Server.
We also need the php code you use when trying to connect.

 

by: edukatePosted on 2009-11-05 at 12:45:04ID: 25753808

Take a look at the moodle thread I've linked to in the OP - unfortunately I haven't got any more descriptive error than that (highlighted in Red)

 

by: TiberiuGalPosted on 2009-11-05 at 12:59:02ID: 25753948

for windows 2008 and iis 7 you can use the web platform installer
http://www.microsoft.com/web/Downloads/platform.aspx
with this tool you can setup your server (iis, php, sqlserver) and you can also install a number of web aplications (including moodle) with just a few clicks.

 

by: edukatePosted on 2009-11-05 at 13:16:16ID: 25754135

Thanks for suggestion TiberiuGal - but as in the start of my other Moodle post that was one of the first things I tried and it didn't work..

 

by: TiberiuGalPosted on 2009-11-05 at 13:30:43ID: 25754257

Sorry, I skiped that line :) I also got dizzy reading that thread.
Please tell me where exactly do you stand now.I mean ...which version of what do you have installed atm?!
Did you manage to connect to the database from a php script (not within moodle)?

 

by: edukatePosted on 2009-11-05 at 13:39:21ID: 25754337

I haven't tried connecting from a php script but this would be a good test - Could you please point me to some details of how to do this?

 

by: IceCodePosted on 2009-11-05 at 13:42:07ID: 25754357

If you want to test create a file called test.php (or something), add the code below and upload to your server.  That should help you nail down what the settings should be.

Easy Hosting Solutions
http://www.HDWebStudios.com

<?PHP
//Database Settings
	define("DBNAME","MoodleDB"); //Name of database
	define("DBSERVER","."); //Server name where the database lives [.] for localhost
	define("DBUSER","Username"); //Username for database
	define("DBPASS","Password"); //Password for database
 
//Create DB Connection
	$conn = mysql_connect(DBSERVER,DBUSER,DBPASS) or die (mysql_error());
	echo "Successfully connected!";
?>

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:

Select allOpen in new window

 

by: edukatePosted on 2009-11-05 at 13:52:58ID: 25754463

Thanks I tried the code by changed each mysql into mssql (mysql service is stopped for now) but get a HTTP error 500

 

by: IceCodePosted on 2009-11-05 at 13:57:54ID: 25754507

mssql_error() is not valid.  FYI.  Change that to something like $conn = mssql_connect(DBSERVER,DBUSER,DBPASS) or die ("wtf");

Doesn't give you the SQL error but you only have a few variables to work with, if you cannot get this to work you may have other problems.  Look at this page:

http://www.helpspot.com/helpdesk/index.php?pg=kb.page&id=13

 

by: edukatePosted on 2009-11-05 at 14:21:50ID: 25754732

Yeap we got other wtf issues..

We don't have the ntwdblib.dll file in PHP ext or even commented in php.ini
If fact these are the only extensions in the ext directory:
php_curl.dll
php_exif.dll
php_gd2.dll
php_gettext.dll
php_imap.dll
php_mbstring.dll
php_mssql.dll
php_mysql.dll
php_mysqli.dll
php_openssl.dll
php_pdo.dll
php_pdo_mysql.dll
php_pdo_sqlite.dll
php_soap.dll
php_tidy.dll
php_wincache.dll
php_xmlrpc.dll
php_zip.dll

 

by: IceCodePosted on 2009-11-05 at 14:24:49ID: 25754759

I see a php_mssql.dll, this issue may not apply to your version.  Double check that.  Make sure MSSQL is enabled in php.ini.  Did you use the windows installer?  If you did you can just run that executable, choose modify (or change) and make sure that the MSSQL extension is installed.

 

by: edukatePosted on 2009-11-05 at 14:35:19ID: 25754854

Yes it's enabled in php.ini

I've tried the test.php connection for mysql (I don't mind it running with either DB at this stage)
I get this error:

Can't open name pipe to host: . pipe: MySQL (2)

What does the (2) mean - could we have two instances of mysql installed and it's getting confused between them? If so how can I confirm / remove them.. There is only one mysql service..

 

by: IceCodePosted on 2009-11-05 at 16:03:26ID: 25755506

 

by: edukatePosted on 2009-11-05 at 16:06:45ID: 25755529

yeap but port 3306 was already being used so we used port 3308 instead...

 

by: IceCodePosted on 2009-11-05 at 16:10:02ID: 25755548

Add the port to DBSERVER, see below.

Easy Hosting Solutions
http://www.HDWebStudios.com/hosting.php

<?PHP
//Database Settings
	define("DBNAME","MoodleDB"); //Name of database
	define("DBSERVER",".:3308"); //Server name where the database lives [.] for localhost
	define("DBUSER","Username"); //Username for database
	define("DBPASS","Password"); //Password for database
 
//Create DB Connection
	$conn = mysql_connect(DBSERVER,DBUSER,DBPASS) or die (mysql_error());
	echo "Successfully connected!";
?>
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:

Select allOpen in new window

 

by: edukatePosted on 2009-11-05 at 16:12:45ID: 25755560

Have done - still get same error tho
Can't open name pipe to host: . pipe: MySQL (2)

 

by: IceCodePosted on 2009-11-05 at 16:17:54ID: 25755600

You may want to try adding "host = localhost" in "my.ini" in mySQL in the client mechine.

[client]
host=localhost

Comment out those:

#protocol = pipe
#socket=mysql

 

by: edukatePosted on 2009-11-05 at 16:24:09ID: 25755628

I updated the client section as above in my.ini on the server mysqy install directory..

There weren't either of the others protocol or socket..

Still no luck

 

by: edukatePosted on 2009-11-05 at 18:45:09ID: 25756243

Anything else we can try to get this working?

 

by: edukatePosted on 2009-11-05 at 20:21:05ID: 25756593

I am told I may need to setup freeTDS to get the database connection working..
I've downloaded a Non-Thread Safe DLL for php5.2 from moodle.org and added it to the \php\ext folder

I added this line to php.ini under [mysql]
extension=php_dblib.dll

created freetds.conf file in c:\ with the following content
[global]
host = 192.serverIP..
port = 3308
client charset = UTF-8
tds version = 8.0
text size = 20971520


Is this required and if so am I on the right track here?

 

by: IceCodePosted on 2009-11-06 at 06:27:25ID: 25759341

This should not be needed unless it is a Plesk thing.  I mean I have this working on a Windows 2008 server here and it just wasn't this difficult.  I am using the standard port, but still doesn't seem to be your problem.  I would start looking at everything I guess.  Can you pull a phpinfo page?  I'm sure you already know this but make a page something.php and put <?PHP phpinfo(); ?> in the file.  Upload and run, do you get the page?

 

by: edukatePosted on 2009-11-08 at 11:36:22ID: 25771553

Yeap I can get phpinfo page and the database section is in the moodle thread too..
I don't see how this can be soo difficult either!

 

by: edukatePosted on 2009-11-09 at 11:38:09ID: 25779188

OK I've decided to start again from scratch using the exact same config as the moodlewindows.za.net guide (win2k3 server, iis6, sql2005, non thread sage php 5.2 fastcgi, freetds) and have followed everything in there exactly, however now we are still getting Error: Database connection failed.

In the phpinfo.php page now there is no database section at all..
I have tried setting up error_log = syslog in php.ini but there are now events written to event log..

Any simple suggestions how I might enable the database section of phpinfo.php and get around this error?

 

by: IceCodePosted on 2009-11-09 at 13:52:51ID: 25780520

Ok.  Sounds like you are using MSSQL then.  Run the PHP install executable, select "Change", drill down and find the MSSQL extension.  Install it.  When you are done, download the attached file, rename .txt to .dll, find it on your system, and replace it with this one (trust me MSSQL will never work if you do not, Google it if you want).  Restart w3svc and rerun phpinfo, make sure it shows up now.  Re-test.

Easy Hosting Solutions
http://www.HDWebStudios.com/hosting.php

 

by: edukatePosted on 2009-11-09 at 14:49:14ID: 25780963

I was following the moodlewindows guide which recommends using freetds over the mssql.dll connection because of a Unicode problem / difference..
I have got this running now and the MSSQL section appears in phpinfo.php now so it seems the only problem is the connection to database.. I'm going to delete the DB and user and create them again and see how I go..
Is there another way to test this with error logging other than the $conn = mysql_connect method as above?
This is currently just giving the wtf message

 

by: IceCodePosted on 2009-11-09 at 16:01:49ID: 25781340

it should be mssql_connect.

 

by: edukatePosted on 2009-11-09 at 16:10:00ID: 25781383

OK I found the problem - The freetds wasn't loading with this:
extension=php_dblib.dll
I added that also this into php.ini
error_log=syslog

That showed an error in Event Log: 18488
Login failed for user 'UserLogin'.  Reason: The password of the account must be changed

I resolved it with:
ALTER LOGIN UserLogin WITH PASSWORD = 'NewPassword' UNLOCK

I then set the password and turned of 'enforce password policy' and could connect to database :)

Thanks for the advice guys - grreat to get it going at last!

 

by: IceCodePosted on 2009-11-09 at 16:48:11ID: 25781564

So basically you left the box checked in SQL to force a password change on next logon.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...