gpt4 book ai didi

php页面无法加载MySQL的数据库

转载 作者:行者123 更新时间:2023-11-29 01:00:47 25 4
gpt4 key购买 nike

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

)

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

如您所见,我使用的是 EasyPHP,并且由于这段代码以前可以正常工作(使用不同的数据库,同时使用手动配置的 apache/mysql),这可能是原因吗?其他信息:我使用 phpmyadmin 创建了数据库,我有 win7

谢谢

最佳答案

不要永远未经验证就从$_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...

关于php页面无法加载MySQL的数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3416623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com