gpt4 book ai didi

php - 当我的文件在 FTP 中运行时无法连接到 MySQL 数据库

转载 作者:行者123 更新时间:2023-11-29 21:57:28 25 4
gpt4 key购买 nike

编辑:[已解决]

问题已解决。我终于发现一个问题了。有一些错误。首先,当我的文件位于 FTP 中时,我尝试连接 IP。要连接到我的 mysql,我必须通过 localhost 写入 db_host,而不是通过 IP。其次,数据库权限几乎没有问题,我只能创建一个名为 test 的数据库,我不知道为什么......所以如果我输入 db_name 作为“test”,一切正常。太奇怪了!

当我在 FTP 中运行 install.php 时,访问 mysql 时遇到一些问题。让我解释一下:当用户安装他的网站时,我的 install.php 应该创建一个数据库。一切正常,我在本地运行 install.php,与 FTP 服务器不同。

让我补充一点,我 100% 确定我的访问是正确的(我的意思是主机、用户名和密码)。否则这个过程在本地(?)中不起作用。

错误代码:

DB ERROR: SQLSTATE[28000] [1045] Access denied for user 'alpha'@'my_host' (using password: YES)

我的 install.php 文件:

    <?php
if (empty($_POST['db_host']) OR empty($_POST['db_name']) OR empty($_POST['db_username']) OR empty($_POST['db_password']))
{ ?>
<?php include("content/head.php"); ?>
<div class="container install-container text-center">
<div class="install-container-border">
<h1 class="exo-bold">
<a><img src="/../img/alpha-logo-small.png"></a></br>
<small class="hvr-shadow-radial">Installation</small></br></br>
<div style="padding-right: 100px; padding-left: 100px">
<div class="install-progress">
<div class="install-progress-bar" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width: 50%;">50%</div>
</div>
</div>
</h1>
<div class="text-left" style="padding-left: 150px">
<form method="post" action="install.php" style="font-family: raleway !important;">
<p>
<div style="height: 30px">Database Host: <input type="text" name="db_host" style="height: 22px"/></div>
<?php if (!empty($_POST['try']) && empty($_POST['db_host'])) {;?> <div class="alert-install alert-danger" role="alert">Database host is required.</div> <?php } ?>
<div style="height: 30px">Database Name: <input type="text" name="db_name" style="height: 22px"/></div>
<?php if (!empty($_POST['try']) && empty($_POST['db_name'])) {;?> <div class="alert-install alert-danger" role="alert">Database name is required.</div> <?php } ?>
<div style="height: 30px">Database Username: <input type="text" name="db_username" style="height: 22px"/></div>
<?php if (!empty($_POST['try']) && empty($_POST['db_username'])) {;?> <div class="alert-install alert-danger" role="alert">Database username is required.</div> <?php } ?>
<div style="height: 30px">Database Password: <input type="password" name="db_password" style="height: 22px"/></br></div>
<?php if (!empty($_POST['try']) && empty($_POST['db_password'])) {;?> <div class="alert-install alert-danger" role="alert">Database password is required.</div> <?php } ?>
<div style="height: 30px">Admin Username: <input type="text" name="adm_username" style="height: 22px"/></div>
<?php if (!empty($_POST['try']) && empty($_POST['admin_username'])) {;?> <div class="alert-install alert-danger" role="alert">Admin username is required.</div> <?php } ?>
<div style="height: 30px">Admin Password: <input type="password" name="adm_password" style="height: 22px"/></br></div>
<?php if (!empty($_POST['try']) && empty($_POST['admin_password'])) {;?>
<div class="alert-install alert-danger" role="alert">Admin password is required.</div> <?php } ?>
<input type="hidden" name="try" value="try"/>
<div style="height: 50px; margin-top: 50px;">
<input type="submit" value="Confirm"style="font-family: raleway !important;"class="btn btn-default btn-alpha text-center"/>
</div>
</p>
</form></br></br>
</div>
</div>
</div>
<?php }
else
{
$dbh = new PDO("mysql:host=".$_POST['db_host'], $_POST['db_username'], $_POST['db_password']);

$dbh->exec("CREATE DATABASE ".$_POST['db_name'].";");
$dbh->exec("USE ".$_POST['db_name'].";");
$dbh->exec("CREATE TABLE admin (id INT PRIMARY KEY NOT NULL AUTO_INCREMENT, username VARCHAR(255) UNIQUE, password VARCHAR(255));");
$dbh->exec("INSERT INTO admin (username, password) VALUES ('".$_POST['adm_username']."','".$_POST['adm_password']."');");

echo "BDD created!";
}
?>

我成功尝试在终端中使用以下命令访问我的 BDD:mysql -h my_host -u alpha -p

最佳答案

您的问题与访问本身有关。请检查远程服务器上的凭据。您的脚本可能在本地运行,因为提供的凭据可能适合您的本地环境。

关于php - 当我的文件在 FTP 中运行时无法连接到 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32996063/

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