Jump to content

Can't connect to mysql DB from PHP code on localhost


MiRed

Recommended Posts

Hi everyone!

 

I am building an application in PHP with MySQL on my localhost. I tried to connect to the database like this:

mysql_connect("localhost", "root", "password");
mysql_select_db("app-db");

 

but the message I get is:

 

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'user'@'localhost' (using password: NO) in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\app\Source\includes\db_connect.php on line 3

 

Warning: mysql_select_db() [function.mysql-select-db]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\app\Source\includes\db_connect.php on line 4

 

Warning: mysql_select_db() [function.mysql-select-db]: A link to the server could not be established in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\app\Source\includes\db_connect.php on line 4

 

I never wrote user 'user' in the whole application but root and even when I comment out that line of mysql_connect, the first error doesn't go away.

The url of the index of the application is localhost/app/Source/index.php

When I make a page: localhost/index2.php with the same content as my db_connec file then there is no error. When I move or copy the db_connect file to localhost/db_connect.php it also gives me those warnings.

Also, when I change things in the code, PHP does not recognize them and nothing changes - meaning, that I can comment out those connect statements and still get the warnings.

Can anyone offer me some advice and help me solve this, please?

 

Thanks in advance!

MiRed

Link to comment
Share on other sites

the 182guy - you are right. I have a separate file with variables, where I filled in the correct values and in the first file I wrote:

global $DB_SERVER,$DB_USER,$DB_USER,$DB_PWD,$DB_NAME;
    
        //try to connect to the server and save db object
        $this->db_link = mysql_connect($DB_SERVER,$DB_USER,$DB_PWD) or die("Auth Subsystem Error1");

I'm using the db_connect.php file somewhere else.

But my question is till not answered - when I go directly go to localhost/app/Source/includes/db_connect.php I get the three warnings?!

Thanks!!

Link to comment
Share on other sites

Actually the best place would be in the same place where you are setting error_reporting to what it currently is (so that you don't need to keep putting the setting into your code.)

 

You should be doing this on a local development system and the best place to set this is in your master php.ini (stop and start your server to get any changes made to the master php.ini to take effect.)

Link to comment
Share on other sites

Now I get loads of warnings like the following:

Notice: Use of undefined constant SMARTY_template_dir - assumed 'SMARTY_template_dir' in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\app\Source\includes\defines.php on line 4

even tho I wrote(or that's the line it's referring to):

define (SMARTY_template_dir, 'C:/Program Files(x86)/Apache Software Foundation/Apache2.2/htdocs/Smarty/Templates');

Summary: I get warnings for undefined constants but that's exactly where I'm defining them!

Link to comment
Share on other sites

Those are actually notices (you will likely see notice, warning, fatal runtime, or fatal parse error messages) and the exact wording of the error message and the code that produces the error is important.

 

If the code that BlueSkyIS posted didn't change the error message at all, then it is likely you didn't save the code change to the actual file running on the server or you have a similar error on a line number close to the previous line with an error.

Link to comment
Share on other sites

If you are still getting an error message, posting it and the relevant code would be the best way of getting help.

 

Your code should not generate ANY error messages during its normal execution, for a few reasons -

 

1) Each statement in your code that generates an error takes 10-20 times longer to execute than if the statement was error free. The error message that gets displayed or logged is just the final stage of the error response code. Even if the error_reporting/display_errors/log_errors settings hide the error messages, php must still handle each error as it occurs.

 

2) The errors are likely getting logged and code that regularly generates a long list of error messages every time it executes quickly produces a multi-gigabyte error log file.

 

3) Code that regularly generates a long list of error messages every time it executes makes finding actual problems harder, such as when you make a typo in a variable name or on a live site when a hacker starts trying to break into your code by feeding it all kinds of unexpected data. You want to get error messages for UNEXPECTED things so that you can find and fix what is causing the problem. You don't want a score of error messages every time you execute your code just because your code has fixable problems in it.

Link to comment
Share on other sites

1-that was interesting, I didnt know that!

2-oh no!

3-totally agree.

There are two problems with posting the code - it's for my work ,it's actually not me who wrote it, but on the real test server it and production it works. I only want it to get to work on my localhost in order for me to be able to develop and test locally and only then upload it to the mercury repository which we are using. I'ts quite complex and loads of code. One file is included in the other and that's why it's hard to find the orignal occurence of the things. Anyway.

I couldn't clone it (the repository) directly into my htdocs so I copied/downloaded  it to my documents and copied it from there to the htdocs. Could this make a problem? Can it be that the server reads the files which are in My Documents??

Link to comment
Share on other sites

The server only reads the files in the document root folder that it has been configured to use.

 

It does sound however like what you are seeing in the browser is a cached version of the page and that changes to the php source are not taking effect immediately.  Try forcing your browser to refresh the page and see if that causes the expected output.

 

How did you obtain and install your local development system? Are there any .htaccess files present that could be modifying the cache settings (either that the web server is doing or that the browser is doing?) Is there a server side byte-code cache present, such as APC?

Link to comment
Share on other sites

I don't really know what cache is... :shy::shrug::confused:

How do I force my browser to refresh? I pressed F5 till now.

Do you mean my localhost? I installed it with the tutorial of : http://www.bicubica.com/apache-php-mysql/index.php, just I am using a bit a newer version than what they describe there -> I installed them all by hand as wamp server didnt let me change the configurations (or I just didnt know how:).

I dont know if there are any .htaccess files. Haven't seen any. But also didnt look for them... :P

What is APC?

Link to comment
Share on other sites

I didn't find any .htaccess files that belong to my current server.

There are lots of cache settings in the php.ini file, but I don't know what to do with them and I don't want to play around with it.

Where do I see if there is a side byte-ode cache present?

What is APC?

Link to comment
Share on other sites

  • 2 weeks later...

After many hours of finding and trying, I found the solution. I was using Zend Studio as my editor and it showed that the file got save but didnt actually. Once I opened the files with Notepad, I saw that nothing had been changed. Hope that helps other people as well. I guess just don't use Zend Studio for localhost stuff.

Good Luck!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.