Jump to content

mysql_connect problem


R_14_14

Recommended Posts

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]

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

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.