MiRed Posted December 28, 2010 Share Posted December 28, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/222823-cant-connect-to-mysql-db-from-php-code-on-localhost/ Share on other sites More sharing options...
desjardins2010 Posted December 28, 2010 Share Posted December 28, 2010 if your using localhost did you set a password? if not try connecting with localhost, root, "" or localhost, "", "" Quote Link to comment https://forums.phpfreaks.com/topic/222823-cant-connect-to-mysql-db-from-php-code-on-localhost/#findComment-1152146 Share on other sites More sharing options...
pcman Posted December 28, 2010 Share Posted December 28, 2010 this is error with your user or host... you need to be sure that your details are ok. modify: be sure that you dont write a pass itws need to look like this: mysql_connect("localhost","root",""); good luck! Quote Link to comment https://forums.phpfreaks.com/topic/222823-cant-connect-to-mysql-db-from-php-code-on-localhost/#findComment-1152148 Share on other sites More sharing options...
MiRed Posted December 28, 2010 Author Share Posted December 28, 2010 I did set a password and I know it's correct because when I put the same code in the file localhost/index2.php it works! Quote Link to comment https://forums.phpfreaks.com/topic/222823-cant-connect-to-mysql-db-from-php-code-on-localhost/#findComment-1152164 Share on other sites More sharing options...
the182guy Posted December 28, 2010 Share Posted December 28, 2010 You're not running the code in your first post, you are running some other code where mysql_connect() is using username 'user' and a blank password. Check you're including the correct dbconnect script. Quote Link to comment https://forums.phpfreaks.com/topic/222823-cant-connect-to-mysql-db-from-php-code-on-localhost/#findComment-1152169 Share on other sites More sharing options...
MiRed Posted December 28, 2010 Author Share Posted December 28, 2010 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!! Quote Link to comment https://forums.phpfreaks.com/topic/222823-cant-connect-to-mysql-db-from-php-code-on-localhost/#findComment-1152180 Share on other sites More sharing options...
the182guy Posted December 28, 2010 Share Posted December 28, 2010 Well if your db_connect.php does not have the user/pass/host variables in it then of course you'll get the warnings because $DB_SERVER, $DB_USER, $DB_PWD are all undefined and will be treated as blank. Quote Link to comment https://forums.phpfreaks.com/topic/222823-cant-connect-to-mysql-db-from-php-code-on-localhost/#findComment-1152184 Share on other sites More sharing options...
PFMaBiSmAd Posted December 28, 2010 Share Posted December 28, 2010 And if you set error_reporting to E_ALL you will also be getting notice error messages about the variables that are not set. Quote Link to comment https://forums.phpfreaks.com/topic/222823-cant-connect-to-mysql-db-from-php-code-on-localhost/#findComment-1152187 Share on other sites More sharing options...
MiRed Posted December 28, 2010 Author Share Posted December 28, 2010 Where do I set the error reporting to E_ALL? Quote Link to comment https://forums.phpfreaks.com/topic/222823-cant-connect-to-mysql-db-from-php-code-on-localhost/#findComment-1152189 Share on other sites More sharing options...
PHPTOM Posted December 28, 2010 Share Posted December 28, 2010 Where do I set the error reporting to E_ALL? Top of the file Quote Link to comment https://forums.phpfreaks.com/topic/222823-cant-connect-to-mysql-db-from-php-code-on-localhost/#findComment-1152190 Share on other sites More sharing options...
PFMaBiSmAd Posted December 28, 2010 Share Posted December 28, 2010 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.) Quote Link to comment https://forums.phpfreaks.com/topic/222823-cant-connect-to-mysql-db-from-php-code-on-localhost/#findComment-1152196 Share on other sites More sharing options...
MiRed Posted December 28, 2010 Author Share Posted December 28, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/222823-cant-connect-to-mysql-db-from-php-code-on-localhost/#findComment-1152199 Share on other sites More sharing options...
BlueSkyIS Posted December 28, 2010 Share Posted December 28, 2010 define ('SMARTY_template_dir', 'C:/Program Files(x86)/Apache Software Foundation/Apache2.2/htdocs/Smarty/Templates'); Quote Link to comment https://forums.phpfreaks.com/topic/222823-cant-connect-to-mysql-db-from-php-code-on-localhost/#findComment-1152211 Share on other sites More sharing options...
MiRed Posted December 28, 2010 Author Share Posted December 28, 2010 Thanks, it just didnt change anything in the warnings... Quote Link to comment https://forums.phpfreaks.com/topic/222823-cant-connect-to-mysql-db-from-php-code-on-localhost/#findComment-1152214 Share on other sites More sharing options...
MiRed Posted December 28, 2010 Author Share Posted December 28, 2010 By the way - this is for the128guy - my db_connect.php looks like this: mysql_connect("localhost", "root", "password"); mysql_select_db("app-db") Quote Link to comment https://forums.phpfreaks.com/topic/222823-cant-connect-to-mysql-db-from-php-code-on-localhost/#findComment-1152218 Share on other sites More sharing options...
PFMaBiSmAd Posted December 28, 2010 Share Posted December 28, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/222823-cant-connect-to-mysql-db-from-php-code-on-localhost/#findComment-1152219 Share on other sites More sharing options...
MiRed Posted December 28, 2010 Author Share Posted December 28, 2010 That could be, tho I opened the file like this: C.....htdocs\app\Source\index.php ?! How can I check which file the server is using/reading? Quote Link to comment https://forums.phpfreaks.com/topic/222823-cant-connect-to-mysql-db-from-php-code-on-localhost/#findComment-1152221 Share on other sites More sharing options...
BlueSkyIS Posted December 28, 2010 Share Posted December 28, 2010 yes, my point was that you need to add quotes around the string you are defining as a constant. Quote Link to comment https://forums.phpfreaks.com/topic/222823-cant-connect-to-mysql-db-from-php-code-on-localhost/#findComment-1152222 Share on other sites More sharing options...
PFMaBiSmAd Posted December 28, 2010 Share Posted December 28, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/222823-cant-connect-to-mysql-db-from-php-code-on-localhost/#findComment-1152239 Share on other sites More sharing options...
MiRed Posted December 28, 2010 Author Share Posted December 28, 2010 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?? Quote Link to comment https://forums.phpfreaks.com/topic/222823-cant-connect-to-mysql-db-from-php-code-on-localhost/#findComment-1152257 Share on other sites More sharing options...
PFMaBiSmAd Posted December 28, 2010 Share Posted December 28, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/222823-cant-connect-to-mysql-db-from-php-code-on-localhost/#findComment-1152270 Share on other sites More sharing options...
MiRed Posted December 28, 2010 Author Share Posted December 28, 2010 I don't really know what cache is... 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... What is APC? Quote Link to comment https://forums.phpfreaks.com/topic/222823-cant-connect-to-mysql-db-from-php-code-on-localhost/#findComment-1152273 Share on other sites More sharing options...
MiRed Posted December 29, 2010 Author Share Posted December 29, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/222823-cant-connect-to-mysql-db-from-php-code-on-localhost/#findComment-1152667 Share on other sites More sharing options...
MiRed Posted January 10, 2011 Author Share Posted January 10, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/222823-cant-connect-to-mysql-db-from-php-code-on-localhost/#findComment-1157290 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.