R_14_14 Posted September 27, 2010 Share Posted September 27, 2010 Hi, I am having trouble connecting to mysql via PHP. I have the following PHP script called mysqlconnect.php: <?php $link = mysql_connect ('localhost','root', 'PASSWORD'); if (!$link) print unsuccessful; else print successful; mysql_close($link); ?> (where I substitute PASSWORD with the real root password) which I try to run by typing http://localhost/mysqlconnect.php into my web browser. However, the screen stays blank and (un)successful is not printed. I am running windows 7 with Apache 2.2, PHP 5.3 and MySQL server 5.1. mysqlconnect.php is in the htdocs folder of the Apache installation directory. As far as I can tell Apache and PHP are configured correctly, the famous helloword.php script works fine. Also, I can see in task manager that Apache and mysql are running. Also, when I access mysql from the command line, the password works fine. The only thing I´m not sure about is username ´root´ - when configuring MySQL it never asked me for a username. Could someone please explain what I´ve done wrong? Sorry if this is a stupid question, I´m new to this stuff. Thanks a lot! [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/214521-mysql_connect-problem/ Share on other sites More sharing options...
PFMaBiSmAd Posted September 27, 2010 Share Posted September 27, 2010 You are probably getting a fatal runtime error because the mysql_connect() function is not defined because the mysql extension is not enabled in your php.ini. You should set error_reporting to E_ALL and display_errors to ON in your master php.ini so that all the errors php detects will be reported and displayed. Stop and start your web server to get any changes made to the master php.ini to take effect and check the values of those two settings using a phpinfo(); statement in case the php.ini that you are changing is not the one that php is using. While you are making changes to the php.ini, uncomment the extension=php_mysql.dll line so that the mysql extension will be enabled. Check in the phpinfo() output that there is a mysql section showing that the extension is enabled. Quote Link to comment https://forums.phpfreaks.com/topic/214521-mysql_connect-problem/#findComment-1116267 Share on other sites More sharing options...
joel24 Posted September 27, 2010 Share Posted September 27, 2010 you need to enclose the string you wish to echo inside quotes unless they are constants... which they're not in that script i.e. <?php $link = mysql_connect ('localhost','root', 'PASSWORD'); if (!$link) print "unsuccessful"; else print :successful"; mysql_close($link); ?> Quote Link to comment https://forums.phpfreaks.com/topic/214521-mysql_connect-problem/#findComment-1116271 Share on other sites More sharing options...
R_14_14 Posted September 27, 2010 Author Share Posted September 27, 2010 OK thanks so far. my script is now as follows: <?php $link = mysql_connect ('localhost', 'root', 'PASSWORD'); if (!$link) { die('Could not connect: ' . mysql_error()); } else { print 'successful'; } mysql_close($link); ?> When I run it, I get the following error message: Warning: mysql_connect(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Europe/Paris' for '2.0/DST' instead in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\mysqlconnect.php on line 3 Warning: mysql_connect(): [2002] A connection attempt failed because the connected party did not (trying to connect via tcp://localhost:3306) in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\mysqlconnect.php on line 3 Warning: mysql_connect(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Europe/Paris' for '2.0/DST' instead in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\mysqlconnect.php on line 3 Warning: mysql_connect(): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\mysqlconnect.php on line 3 Warning: main(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Europe/Paris' for '2.0/DST' instead in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\mysqlconnect.php on line 3 Fatal error: Maximum execution time of 30 seconds exceeded in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\mysqlconnect.php on line 3 I changed the php.ini file, so that error reporting is on and set to E_ALL, and the mysql_connect extension is enabled. Here is (hopefully) relevant info from phpinfo(); : apache2handeler: Hostname:Port LAPTOP.home:0 Apache Environment: HTTP_HOST localhost SystemRoot C:\Windows SERVER_NAME localhost DOCUMENT_ROOT C:/Program Files (x86)/Apache Software Foundation/Apache2.2/htdocs Core: (1st value is local value, 2nd=master) display_errors On On display_startup_errors Off Off doc_root no value no value docref_ext no value no value docref_root no value no value error_reporting 22527 22527 SMTP localhost localhost PHP variables: _SERVER["HTTP_HOST"] localhost _SERVER["SystemRoot"] C:\Windows _SERVER["SERVER_SOFTWARE"] Apache/2.2.16 (Win32) PHP/5.3.3 _SERVER["SERVER_NAME"] localhost _SERVER["DOCUMENT_ROOT"] C:/Program Files (x86)/Apache Software Foundation/Apache2.2/htdocs Could someone please tell me whats wrong? Cheers! Quote Link to comment https://forums.phpfreaks.com/topic/214521-mysql_connect-problem/#findComment-1116333 Share on other sites More sharing options...
R_14_14 Posted September 27, 2010 Author Share Posted September 27, 2010 and this is mysql heading from phpinfo(); : MySQL Support enabled Active Persistent Links 0 Active Links 0 Client API version mysqlnd 5.0.7-dev - 091210 - $Revision: 300533 $ Directive Local Value Master Value mysql.allow_local_infile On On mysql.allow_persistent On On mysql.connect_timeout 60 60 mysql.default_host no value no value mysql.default_password no value no value mysql.default_port no value no value mysql.default_socket no value no value mysql.default_user no value no value mysql.max_links Unlimited Unlimited mysql.max_persistent Unlimited Unlimited mysql.trace_mode Off Off Quote Link to comment https://forums.phpfreaks.com/topic/214521-mysql_connect-problem/#findComment-1116334 Share on other sites More sharing options...
joel24 Posted September 28, 2010 Share Posted September 28, 2010 you need to set the timezone in the PHP.ini file, look for date.timezone = "YOUR_TIME_ZONE" and set it to your timezone, a list of timezones can be found here Quote Link to comment https://forums.phpfreaks.com/topic/214521-mysql_connect-problem/#findComment-1116528 Share on other sites More sharing options...
R_14_14 Posted September 28, 2010 Author Share Posted September 28, 2010 Thanks for your replies. I have now tracked down the error further, seemed wise to start a new post: http://www.phpfreaks.com/forums/index.php/topic,311220.0.html Quote Link to comment https://forums.phpfreaks.com/topic/214521-mysql_connect-problem/#findComment-1116686 Share on other sites More sharing options...
PFMaBiSmAd Posted September 28, 2010 Share Posted September 28, 2010 Actually that would be unwise because that looses the history of what you have tried and how you got to this point. Stick to one thread for the duration of one problem. It's the same error you posted in this thread, if you had bothered to read it. Quote Link to comment https://forums.phpfreaks.com/topic/214521-mysql_connect-problem/#findComment-1116720 Share on other sites More sharing options...
R_14_14 Posted September 28, 2010 Author Share Posted September 28, 2010 I know it's essentially the same problem as I posted in this thread, because I myself posted the other thread......... which you could have seen....... had you bothered to read it. The reason I started a new thread is because I started with severeral problems, most of which I have now eliminated, boiling it down to the core issue. These secondary problems (which were easily eliminated) would only confuse the reader, hence I started the new thread which deals only with the core issue at hand. Quote Link to comment https://forums.phpfreaks.com/topic/214521-mysql_connect-problem/#findComment-1116728 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.