Jump to content

PHP not connecting to MySQL?


bluekid239

Recommended Posts

For some odd reason beyond my knowledge, my PHP script will not connect to the MySQL server, and then stops running all-together. This is my code:

<html>	<head>		<title>Testing MySQL Connection</title>	</head>	<body>		<h1>Hello World!</h1>		<?php		$dat = mysql_connect("localhost:3306","root","******");		if(!$dat){		  die("Could not connect to MySQL: ".mysql_error());		}		else{		  echo "Connected successfully!";		}		mysql_close($dat);		?>	</body></html>

When I load this (by going to http://localhost/TestPHP/testSQL.php) , the following is in the source code of the return html:

<html>	<head>		<title>Testing MySQL Connection</title>	</head>	<body>		<h1>Hello World!</h1>

Could the PHP script be timing out, or is there some odd connection problem? The max_execution_time setting in php.ini is at 30 seconds, and it doesn't take very long to load (a few seconds). FastCGI allows it to run much longer (15 minutes) without communicating. I determined that "servername" was "localhost:3306" because that was the port I set it on, and when I queried the server with "select user()" it returned "root@localhost", which was how I determined "username" as well. Am I misunderstanding something? Can someone point me in the right direction?*EDIT* Now that I try again, I'm not getting ANY HTML returns. I get an HTTP 500 Internal Server Error. Here's the description:This error (HTTP 500 Internal Server Error) means that the website you are visiting had a server problem which prevented the webpage from displaying.*Note* the password field in the source code is starred to protect my password.

Link to comment
Share on other sites

Well, I turned off Friendly Error Messages in IE, but it showed me the same webpage it did before I started getting the HTTP 500 page. I went to the php.ini file and modified these settings to the values shown:error_reporting = E_ALL | E_STRICTdisplay_errors = Ondisplay_startup_errors = Onhtml_errors = OnI also modified the file again to include an echo statement before the connect statement, and it worked perfectly fine. However, the echo statement I placed just after the connect statement didn't happen. I found the error logs, and I'm trying to make sense of them now. This is the most recent line I found (with the descriptions added for clarity):DATE: 2010-03-19TIME: 17:47:54S-IP: ::1CS-METHOD: GETCS-URI-STEM: /TestPHP/testSQL.php -CS-URI-QUERY: 80 -S-PORT: ::1CS-USERNAME C-IP CS(User-Agent): Mozilla/4.0+(compatible;+MSIE+8.0;+Windows+NT+6.1;+WOW64;+Trident/4.0;+SLCC2;+.NET+CLR+2.0.50727;+.NET+CLR+3.5.30729;+.NET+CLR+3.0.30729;+Media+Center+PC+6.0)SC-STATUS: 500SC-SUBSTATUS: 0SC-WIN32-STATUS: 0TIME-TAKEN: 10or exactly as it appeared in the log:2010-03-19 17:47:54 ::1 GET /TestPHP/testSQL.php - 80 - ::1 Mozilla/4.0+(compatible;+MSIE+8.0;+Windows+NT+6.1;+WOW64;+Trident/4.0;+SLCC2;+.NET+CLR+2.0.50727;+.NET+CLR+3.5.30729;+.NET+CLR+3.0.30729;+Media+Center+PC+6.0) 500 0 0 10The new line in there was an automatic thing due to the length of the phrase. I can't make sense of it myself.EDIT:: I finally managed to squeeze an error report out of the PHP server. This is the error I received:PHP Fatal error: Call to undefined function mysql_connect() in C:\inetpub\wwwroot\TestPHP\testSQL.php on line 9 I note that in making sure functionality with MySQL was secure, I found that I was missing what appears to me as a key file: libmysql.dll.I can't find it anywhere in C:\PHP. The manual says it comes in the Windows version, but I'm missing it. I downloaded the zip file as opposed to the downloader and installed manually. Would that make a difference?

Link to comment
Share on other sites

Well, I turned off Friendly Error Messages in IE, but it showed me the same webpage it did before I started getting the HTTP 500 page. I went to the php.ini file and modified these settings to the values shown:error_reporting = E_ALL | E_STRICTdisplay_errors = Ondisplay_startup_errors = Onhtml_errors = OnI also modified the file again to include an echo statement before the connect statement, and it worked perfectly fine. However, the echo statement I placed just after the connect statement didn't happen. I found the error logs, and I'm trying to make sense of them now. This is the most recent line I found (with the descriptions added for clarity):DATE: 2010-03-19TIME: 17:47:54S-IP: ::1CS-METHOD: GETCS-URI-STEM: /TestPHP/testSQL.php -CS-URI-QUERY: 80 -S-PORT: ::1CS-USERNAME C-IP CS(User-Agent): Mozilla/4.0+(compatible;+MSIE+8.0;+Windows+NT+6.1;+WOW64;+Trident/4.0;+SLCC2;+.NET+CLR+2.0.50727;+.NET+CLR+3.5.30729;+.NET+CLR+3.0.30729;+Media+Center+PC+6.0)SC-STATUS: 500SC-SUBSTATUS: 0SC-WIN32-STATUS: 0TIME-TAKEN: 10or exactly as it appeared in the log:2010-03-19 17:47:54 ::1 GET /TestPHP/testSQL.php - 80 - ::1 Mozilla/4.0+(compatible;+MSIE+8.0;+Windows+NT+6.1;+WOW64;+Trident/4.0;+SLCC2;+.NET+CLR+2.0.50727;+.NET+CLR+3.5.30729;+.NET+CLR+3.0.30729;+Media+Center+PC+6.0) 500 0 0 10The new line in there was an automatic thing due to the length of the phrase. I can't make sense of it myself.EDIT:: I finally managed to squeeze an error report out of the PHP server. This is the error I received:PHP Fatal error: Call to undefined function mysql_connect() in C:\inetpub\wwwroot\TestPHP\testSQL.php on line 9 I note that in making sure functionality with MySQL was secure, I found that I was missing what appears to me as a key file: libmysql.dll.I can't find it anywhere in C:\PHP. The manual says it comes in the Windows version, but I'm missing it. I downloaded the zip file as opposed to the downloader and installed manually. Would that make a difference?
Try to install wamp then you get php, mysql, phpmyadmin and apache installed and configured, no worries about missing files or settings. I think it should be www.wampserver.com not sure google it.
Link to comment
Share on other sites

Try to install wamp then you get php, mysql, phpmyadmin and apache installed and configured, no worries about missing files or settings. I think it should be www.wampserver.com not sure google it.
Does it make a difference that I use IIS and not apache? I'm still not sure what phpmyadmin is.
Link to comment
Share on other sites

Does it make a difference that I use IIS and not apache? I'm still not sure what phpmyadmin is.
phpmyadmin is a graphic version of mysql consol, which is very handy sometimes. If you install wamp I would bother changing from apache.
Link to comment
Share on other sites

You shouldn't need libmysql.dll, you do need a file in the extensions directory called php_mysql.dll, and you need to make sure that extension is enabled in php.ini. You also need MySQL installed if you don't have it, which you can download from mysql.org. Most packages which include PHP also include MySQL, but it's good to install everything yourself so that you understand how it communicates and how it's set up.

Link to comment
Share on other sites

You shouldn't need libmysql.dll, you do need a file in the extensions directory called php_mysql.dll, and you need to make sure that extension is enabled in php.ini. You also need MySQL installed if you don't have it, which you can download from mysql.org. Most packages which include PHP also include MySQL, but it's good to install everything yourself so that you understand how it communicates and how it's set up.
I have MySQL installed and set up (I can run the command-line client perfectly fine and do everything with it). In fact, I already have a database and a couple of tables set up and ready to be read/written to by my PHP script. I'm pretty sure I have php_mysql.dll enabled. Here is the list of extensions, exactly as it appears in php.ini:;extension=php_bz2.dll;extension=php_curl.dll;extension=php_dba.dll;extension=php_exif.dll;extension=php_fileinfo.dll;extension=php_gd2.dll;extension=php_gettext.dll;extension=php_gmp.dll;extension=php_intl.dll;extension=php_imap.dll;extension=php_interbase.dll;extension=php_ldap.dll;extension=php_mbstring.dll;extension=php_ming.dll;extension=php_mssql.dll;extension=php_mysql.dll;extension=php_mysqli.dll;extension=php_oci8.dll ; Use with Oracle 10gR2 Instant Client;extension=php_oci8_11g.dll ; Use with Oracle 11g Instant Client;extension=php_openssl.dll;extension=php_pdo_firebird.dll;extension=php_pdo_mssql.dll;extension=php_pdo_mysql.dll;extension=php_pdo_oci.dll;extension=php_pdo_odbc.dll;extension=php_pdo_pgsql.dll;extension=php_pdo_sqlite.dll;extension=php_pgsql.dll;extension=php_phar.dll;extension=php_pspell.dll;extension=php_shmop.dll;extension=php_snmp.dll;extension=php_soap.dll;extension=php_sockets.dll;extension=php_sqlite.dll;extension=php_sqlite3.dll;extension=php_sybase_ct.dll;extension=php_tidy.dll;extension=php_xmlrpc.dll;extension=php_xsl.dll;extension=php_zip.dll
Link to comment
Share on other sites

The semicolon at the beginning of the line comments out that line, you need to remove the semicolon and restart the server to enable the extension. You can verify through the phpinfo page which extensions are loaded, if you see mysql listed on that page then it's loaded and the functions will be defined.

Link to comment
Share on other sites

The semicolon at the beginning of the line comments out that line, you need to remove the semicolon and restart the server to enable the extension. You can verify through the phpinfo page which extensions are loaded, if you see mysql listed on that page then it's loaded and the functions will be defined.
*sigh* Once again I fail to see the obvious. (if I could find a headsmack emoticon I would use it) I guess I didn't fully read the intro at the top of the php.ini file. I figured out my problem, though:Problem A: php_mysql.dll was disabled.Problem B: extension_dir was not set, so it could not find any of the extensions.SOLUTION: I rid the two statements of their ; prefix.Now it works, and I am satisfied. Thank you for helping me out, I greatly appreciate your time!
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...