Connecting Tech Pros Worldwide Forums | Help | Site Map

connecting to MYSQL using PHP and Apache

Newbie
 
Join Date: Jun 2007
Posts: 15
#1: Jun 17 '07
I trying to configure PHP to run MYSQL and it isn't working. I've spend many hours trying to figure this out. I've made changes to the php.ini file and the httpd.conf file and it no longer gives me an error, but it still doesn't connect to the database. It gets to right before the mysql_connect statement where it says "about to connect", and then instead of the error message, I get nothing. It just sits there.

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2.  
  3. <html>
  4. <head>
  5. <title>Insert to Database</title>
  6. </head>
  7. <body>
  8.  
  9.  
  10. <?php
  11.  
  12. $username = "root";
  13. $password = "krustyrules";
  14. $database = "mysql";
  15.  
  16. echo 'about to connect';
  17.  
  18. $link = mysql_connect("localhost:3306",$username,$password);
  19.  
  20. if(!$link) {
  21.       die('Could not connect: ' . mysql_error());
  22. }
  23. echo 'Connected Successfully';
  24. mysql_select_db($database) or die( "Unable to select database");
  25.  
  26. $firstname = $_POST["fname"];
  27. $lastname = $_POST["lname"];
  28. $emailadd = $_POST["email"];
  29.  
  30. $query = "INSERT INTO contacts VALUES ('',$firstname, $lastname, $emailadd)";
  31.  
  32. mysql_query($query);
  33.  
  34. mysql_close($link);
  35.  
  36. ?><br />
  37. You are <?php echo $_POST["fname"]; ?>.<br/>
  38.  
  39. </body>
  40. </html>
  41.  
  42.  

Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,751
#2: Jun 18 '07

re: connecting to MYSQL using PHP and Apache


Have you checked the output of the phpinfo() function for the MySQL info?
Newbie
 
Join Date: Jun 2007
Posts: 15
#3: Jun 18 '07

re: connecting to MYSQL using PHP and Apache


yes nothing there. could it be because I have windows XP and the phpinfo says I have windows NT???
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,751
#4: Jun 18 '07

re: connecting to MYSQL using PHP and Apache


No, Windows XP uses the NT kernel, so that's all normal.

Make sure you've done all the following:
  • Remove the ; from the line ';extension=php_mysql.dll' in php.ini
  • Add the file 'libmysql.dll', from your PHP root dir, to the PATH enviroment variable. Or just copy the file to your windows directory.
  • Restart the apache server.

If thats all complete MySQL should work.
Newbie
 
Join Date: Jun 2007
Posts: 15
#5: Jun 18 '07

re: connecting to MYSQL using PHP and Apache


it still doesn't work......any suggestions?
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,751
#6: Jun 18 '07

re: connecting to MYSQL using PHP and Apache


Are you sure the MySQL server is running ok?

You can also try to follow the three steppes I posted earlyer, but this time add the line, 'extension=php_mysqli.dll' and copyt the file 'libmysqli.dll' to your windows dir.
Newbie
 
Join Date: Jun 2007
Posts: 15
#7: Jun 18 '07

re: connecting to MYSQL using PHP and Apache


i've tried the three previous steps before, they didn't work. could it have anything to do with apache? when i test apache i get the screen "It works!", is that right or should i get the other screen?
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,751
#8: Jun 18 '07

re: connecting to MYSQL using PHP and Apache


That is the default screen apache puts up when it has been successfully installed. The apache setup should not have anything to do with MySQL. MySQL is loaded by PHP.

Try to following code and see what happens.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.   echo mysql_connect("localhost", "root", "YOURPWHERE");
  3. ?>
  4.  
If MySQL works, this should print "resource# 1", or something similar. Else, is should print a message telling you that the mysql_connect function is invalid.

If something else happens, it is likely that the PHP connection to MySQL works, but that there is a problem with the MySQL server itself.

In any case, copy the output and show us.
Newbie
 
Join Date: Jun 2007
Posts: 15
#9: Jun 18 '07

re: connecting to MYSQL using PHP and Apache


It shows a blank screen and "done" in the status bar. nothing on the screen at all. I have MySQL Administrator on and it says that MySQL server is running. I also print out the phpinfo page and there is nothing with MySQL on there either.




Quote:

Originally Posted by Atli

That is the default screen apache puts up when it has been successfully installed. The apache setup should not have anything to do with MySQL. MySQL is loaded by PHP.

Try to following code and see what happens.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.   echo mysql_connect("localhost", "root", "YOURPWHERE");
  3. ?>
  4.  
If MySQL works, this should print "resource# 1", or something similar. Else, is should print a message telling you that the mysql_connect function is invalid.

If something else happens, it is likely that the PHP connection to MySQL works, but that there is a problem with the MySQL server itself.

In any case, copy the output and show us.

Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,751
#10: Jun 19 '07

re: connecting to MYSQL using PHP and Apache


Ok, try this.
Find the line "extension_dir" in your php.ini and point it to the 'ext' directory in your PHP install dir. (In my case 'D:\Program Files\PHP\ext\')
And then restart you Apache server.

Then create a .php file containing the following, and run it.
If everything works you should get a resource #, if not give us the error.
Expand|Select|Wrap|Line Numbers
  1. error_reporting(E_ALL);
  2. echo mysql_connect("localhost", "root", "ROOTPWD") or die(mysql_error());
  3.  
Newbie
 
Join Date: Jun 2007
Posts: 15
#11: Jun 20 '07

re: connecting to MYSQL using PHP and Apache


Got it!!!! Thanks a lot! I didn't have the php.ini file in the C:\Windows dir. I also specified the location of the php.ini file in apache htppd.conf.
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,751
#12: Jun 20 '07

re: connecting to MYSQL using PHP and Apache


Quote:

Originally Posted by jpk872

Got it!!!! Thanks a lot! I didn't have the php.ini file in the C:\Windows dir. I also specified the location of the php.ini file in apache htppd.conf.

Ahh ok, glad it worked out.
Reply