php页面无法加载MySQL数据库

发布于 2024-09-13 01:27:41 字数 1612 浏览 2 评论 0原文

我在尝试将 .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 29

Notice: Undefined variable: db_user in
C:\Program Files
(x86)\EasyPHP-5.3.2\www\prtcl\index.php on line 29

Notice: Undefined variable:
db_password in C:\Program Files
(x86)\EasyPHP-5.3.2\www\prtcl\index.php on line 29

Warning: 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 29

Warning: 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 29

Fatal 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

∞梦里开花 2024-09-20 01:27:41

切勿在未经验证的情况下永远$_POST获取您的数据库凭据。这是这样一个糟糕的想法,这就是导致您错误的原因,因为这些键没有在$_POST中定义,但它们可能结果可能是灾难性的!

尝试将其放入 connect.php 中,

$db_host = "localhost";
$db_user = "root";
$db_password = "";
$db_name = "prtcl";

$db = mysql_connect($db_host, $db_user, $db_password);
mysql_select_db($db_name, $db);
if(!$db) {
    die('Could not connect: ' . mysql_error());
}

然后在其他页面中使用:

require_once("path/to/connect.php");

// ... whatever else you do on this page...

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

$db_host = "localhost";
$db_user = "root";
$db_password = "";
$db_name = "prtcl";

$db = mysql_connect($db_host, $db_user, $db_password);
mysql_select_db($db_name, $db);
if(!$db) {
    die('Could not connect: ' . mysql_error());
}

then in your other pages use:

require_once("path/to/connect.php");

// ... whatever else you do on this page...
爱殇璃 2024-09-20 01:27:41

我会找出为什么 $db_host 变量没有被定义。这可能是您的表单输入有问题。检查该输入字段的名称是否确实是“localhost”。另外,正如 Mark Ba​​ker 上面提到的,您确实需要进行一些输入清理以防止 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文