php页面无法加载MySQL数据库
我在尝试将 .php 与 mysql 连接时遇到一些问题。 这是connection.php代码
<?php
$db_host =$_POST['localhost'];
$db_user =$_POST['root'];
$db_password = "";
$db_name = "prtcl";
?>
,这是我实际使用连接的页面
<?
include("connection.php");
?>
...
<?php
$db = mysql_connect($db_host, $db_user, $db_password);
mysql_select_db($db_name, $db);
if (!$db)
{
die('Could not connect: ' . mysql_error());
}
mysql_close($db);
?>
<body>
...
,这就是我尝试加载它时得到的内容(第29行是这个:
$db = mysql_connect($db_host, $db_user, $db_password);
)
注意:未定义的变量:db_host in C:\程序文件 (x86)\EasyPHP-5.3.2\www\prtcl\index.php 第 29 行
注意:未定义的变量:db_user in C:\程序文件 (x86)\EasyPHP-5.3.2\www\prtcl\index.php 第 29 行
注意:未定义的变量: C:\Program Files 中的 db_password (x86)\EasyPHP-5.3.2\www\prtcl\index.php 第 29 行
警告:mysql_connect() [function.mysql-connect]: [2002] A 连接尝试失败,因为 关联方没有(试图 通过 tcp://localhost:3306 连接) C:\程序文件 (x86)\EasyPHP-5.3.2\www\prtcl\index.php 第 29 行
警告:mysql_connect() [function.mysql-connect]:一个连接 尝试失败,因为已连接 当事人在事后没有做出适当的回应 一段时间,或既定的 连接失败,因为已连接 主机未能响应。在 C:\程序文件 (x86)\EasyPHP-5.3.2\www\prtcl\index.php 第 29 行
致命错误:最大执行时间 C:\Program 中超过 30 秒 文件 (x86)\EasyPHP-5.3.2\www\prtcl\index.php 第 29 行
,你可以看到我正在使用 EasyPHP,因为这个代码以前曾经工作过(使用不同的数据库,同时使用手动配置的 apache /mysql),可能是这个原因吗?其他信息:我使用 phpmyadmin 创建了数据库,我有 win7
谢谢
I'm having some problems while trying to connect a .php with mysql.
here's the connection.php code
<?php
$db_host =$_POST['localhost'];
$db_user =$_POST['root'];
$db_password = "";
$db_name = "prtcl";
?>
and this is the page where I actually use the connection
<?
include("connection.php");
?>
...
<?php
$db = mysql_connect($db_host, $db_user, $db_password);
mysql_select_db($db_name, $db);
if (!$db)
{
die('Could not connect: ' . mysql_error());
}
mysql_close($db);
?>
<body>
...
that's what I get when I try to load it ( line 29 is this one:
$db = mysql_connect($db_host,
$db_user, $db_password);
)
Notice: Undefined variable: db_host in
C:\Program Files
(x86)\EasyPHP-5.3.2\www\prtcl\index.php on line 29Notice: Undefined variable: db_user in
C:\Program Files
(x86)\EasyPHP-5.3.2\www\prtcl\index.php on line 29Notice: Undefined variable:
db_password in C:\Program Files
(x86)\EasyPHP-5.3.2\www\prtcl\index.php on line 29Warning: mysql_connect()
[function.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)\EasyPHP-5.3.2\www\prtcl\index.php on line 29Warning: mysql_connect()
[function.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)\EasyPHP-5.3.2\www\prtcl\index.php on line 29Fatal error: Maximum execution time of
30 seconds exceeded in C:\Program
Files
(x86)\EasyPHP-5.3.2\www\prtcl\index.php on line 29
as you can see I'm using EasyPHP and, since this very code used to work before (with a different db, while using manually configured apache/mysql), may be that the reason? Other infos: I made the db using phpmyadmin and I have win7
thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
切勿在未经验证的情况下永远从
$_POST
获取您的数据库凭据。这是这样一个糟糕的想法,这就是导致您错误的原因,因为这些键没有在$_POST
中定义,但它们可能结果可能是灾难性的!尝试将其放入
connect.php
中,然后在其他页面中使用:
Don't ever take your db credentials from
$_POST
without validation. This is such a terrible idea and this is what's causing your errors, as these keys are not being defined in$_POST
, but they could be and the results could be disastrous!try putting this in
connect.php
then in your other pages use:
我会找出为什么 $db_host 变量没有被定义。这可能是您的表单输入有问题。检查该输入字段的名称是否确实是“localhost”。另外,正如 Mark Baker 上面提到的,您确实需要进行一些输入清理以防止 sql 注入攻击。另外,是否有任何原因无法将数据库主机信息硬编码到配置文件中?
I would find out why the $db_host variable is not getting defined. That might be a problem with your form input. Check to see that the name of that input field really is 'localhost'. Also, as Mark Baker mentioned above, you really want to do some input cleansing to guard against sql injection attacks. Plus, is there any reason you can't hardcode the db host info into a configuration file?