本地开发环境,php & mysql 可以工作,但不能一起使用
我花了一整天的时间尝试使用以下命令设置开发环境:
- PHP 版本 5.3.3
- apache 2.2
- mySQL Server 5.1
- Windows 7
本地主机呈现 php 很好,我可以通过命令行客户端使用 mySQL,但是 每当我尝试通过 PHP 与 mySQL 交互时,都会出现 30 秒的“等待本地主机”,然后出现一个空白的白色浏览器。
- 我尝试运行仅连接到数据库的测试脚本。
- 我尝试使用“root”和正确的密码通过 phpMyAdmin 登录。
- mySQL 服务正在运行。
- 服务器错误日志中没有与失败的连接相关的条目。
- 我的 php.ini 文件中有“extension=php_mysql.dll”行,当我运行“phpinfo()”时,所有 mySQL 信息都会显示。
- “.../php”和“.../MySQL Server 5.1/bin”包含在我的路径中。
我基本上按照此处的说明来设置整个过程。我已经尝试了两次我能想到的一切,所以任何新鲜的想法将不胜感激。
这是我用来连接到数据库的代码:
<?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 />';
}
?>
它是我发布的链接中示例的一部分。 “test”是一个有效的数据库。
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 posted. 'test' is a valid DB.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用 WAMP 或 XAMPP
try using WAMP or XAMPP
我不确定问题是什么,但我开始使用 www.webdevelopersnotes.com 并能够使其全部正常工作。
快乐的一天!
对于其他想要完成设置的人,我建议遵循这些指示。它们似乎是最新的,并且比我发现的其他网站解释了更多的“原因”。
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.
这是 Windows 7 中默认主机文件的问题(或者 MySQL 缺乏 IPv6 支持)。您需要将 IPv6 环回替换为 IPv4 环回。
使用管理员权限打开
C:\Windows\System32\drivers\etc\hosts
(如有必要,可以暂时取消保护)。查找 IPv6:
::1 localhost
更改为 IPv4:
127.0.0.1 localhost
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