Local dev environment, php & mysql work, but not together
I've spent the whole day trying to set up development environment using:
- PHP Version 5.3.3
- apache 2.2
- mySQL Server 5.1
- Windows 7
The localhost renders the php fine, and I can work with mySQL via the command line client, but whenever I try to interact with mySQL through PHP I get a 30 second "waiting for localhost" and then a blank white browser.
- I've tried running tests scripts that just connect to the database.
- I've tried loggin-in via phpMyAdmin using 'root' and the correct password.
- The mySQL service IS running.
- No entries in the server error-log associated with the failed connection.
- I have the line "extension=php_mysql.dll" in my php.ini file and when I run "phpinfo()" all the mySQL information shows up.
- ".../php" and ".../MySQL Server 5.1/bin" are included in my PATH.
I basically followed the instructions here to set the whole thing up. I've tried everything I can think of twice so any fresh ideas would be greatly appreciated.
This is the code I'm using to connect to the database:
<?php
# Define MySQL Settings
define("MYSQL_HOST", "localhost");
define("MYSQL_USER", "root");
define("MYSQL_PASS", "devpass");
define("MYSQL_DB", "test");
$conn = mysql_connect("".MYSQL_HOST."", "".MYSQL_USER."", "".MYSQL_PASS."") or die(mysql_error());
mysql_select_db("".MYSQL_DB."",$conn) or die(mysql_error());
$sql = "SELECT * FROM test";
$res = mysql_query($sql);
while ($field = mysql_fetch_array($res))
{
$id = $field['id'];
$name = $field['name'];
echo 'ID: ' . $field['id'] . '<br />';
echo 'Name: ' . $field['name'] . '<br /><br />';
}
?>
It's strait out of the example from the link I poste开发者_如何学Cd. 'test' is a valid DB.
try using WAMP or XAMPP
I'm not sure what the problem was, but I started over using the directions from www.webdevelopersnotes.com and was able to get it all working.
Happy Day!
Anybody else looking to go through the set up I would suggest following these directions. They seem to be the most up to date and explain a lot more "why" than the other sites I found.
This is a problem with the default hosts file in windows 7 (or with MySQL's lack of IPv6 support). You will need to replace the IPv6 loopback with an IPv4 loopback instead.
Open C:\Windows\System32\drivers\etc\hosts
with admin privileges (if necessarily, you can temporarily de-protected it).
Find IPv6: ::1 localhost
Change to IPv4: 127.0.0.1 localhost
精彩评论