【问题标题】:Impossible to connect to database in host with php无法使用php连接到主机中的数据库
【发布时间】:2016-04-05 07:05:17
【问题描述】:

大家好,我正在使用 PHP 开发一个小型 Web 应用程序,我可以在其中登录并向数据库添加一些东西,在我的 localhost 机器上一切正常 所以我将它上传到我的托管服务器,它与我的本地主机具有相同的 dbname、dbuser、dbpassword .. 但是当我用用户名和密码点击登录按钮时 我收到此错误:

我知道主机已经启动并正在运行,因为当我访问网址时:

gorydev.net.ve/SistemaMatilcell

这是 login.php 的代码

<?php 
session_start();

if(isset($_SESSION['usuario'])){
    header('Location: index.php');
}

$errores = '';

if($_SERVER['REQUEST_METHOD'] == 'POST'){
    $usuario = filter_var(strtolower($_POST['usuario']), FILTER_SANITIZE_STRING);
    $password = $_POST['password'];
    $password = filter_var($password, FILTER_SANITIZE_STRING);

    require '/db/connect.php';

    $statement = $conexion->prepare('SELECT * FROM empleados WHERE usuario = :usuario AND password = :password');
    $statement->execute(array(
        ':usuario' => $usuario,
        ':password' => $password
    ));

    $resultado = $statement->fetch();

    if($resultado != false){
        $_SESSION['usuario'] = $usuario;
        header('Location: index.php');
    }else{
        $errores .= '<li>Datos incorrectos.</li>';
                echo $errores;
    }
}

这是 connect.php 文件:

<?php
use Project\Helpers\Config;
require 'app/Config.php';

$config = new Config;
$config->load('config.php');

try {
    //Datos para realizar la conexion
    //$dbhost = $config->get('db.hosts.local');
        $dbhost = 'localhost';
    $dbname = $config->get('db.name'); 
    $dbuser = $config->get('db.user'); 
    $dbpass = $config->get('db.password'); 

    $conexion = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);

} catch (PDOException $e) {
    echo "Error " . $e->getMessage();
}   
?>

config.php 文件:

<?php  
return [
    'db' => [
        'hosts' => [
            'local' => 'localhost',
            'externo' => '175.99.155.194',
        ],
        'name' => 'cl55-cell',
        'user' => 'cl55-cell',
        'password' => 'matilcell12'
    ],
    'mail' => [
        'host' => 'smtp.gmail.com'
    ]
];
?>

和我的 Config.php 类:

<?php 
/*Esta clase permite cargar las configuraciones del sistema*/
namespace Project\Helpers;
class Config
{
    protected $data;
    protected $default = null;
    public function load($file){
        $this->data = require $file;
    }
    public function get($key, $default = null){
        $this->default = $default;
        $segments = explode('.', $key);
        $data = $this->data;

        foreach ($segments as $segment) {
            if(isset($data[$segment])){
                $data = $data[$segment];
            }else{
                $data = $this->default;
                break;
            }
        }
        return $data;
    }
    public function exists($key){
        return $this->get($key) !== $this->default;
    }
}
?>

我的主机中的数据库如下所示:

带有错误报告:

警告:需要(/db/connect.php):无法打开流:第 17 行的 /home/sites/gorydev.net.ve/public_html/SistemaMatilcell/login.php 中没有此类文件或目录致命错误:require():在 /home/sites/gorydev.net.ve 中打开所需的 '/db/connect.php' (include_path='.:/usr/share/pear55:/usr/share/php') 失败/public_html/SistemaMatilcell/login.php 第 17 行 /db/connect.php/

我在我的本地电脑上重复它工作正常.. 相同的 dbname 相同的用户名和密码

【问题讨论】:

  • 取决于 connect.php 里面的内容 - 你的路径可能不正确 - 检查错误 php.net/manual/en/function.error-reporting.php
  • 请开启错误报告(见顶答案here)并检查您收到的错误信息。这将极大地帮助确定问题所在。
  • 请使用 PHP 的built-in functions 来处理密码安全问题。如果您使用的 PHP 版本低于 5.5,您可以使用 password_hash() compatibility pack
  • 大家好,感谢您的回复我更新了我的问题,以便您可以查看其余文件@Fred-ii-
  • 所以我更新了代码以启用错误报告,这就是我得到的:警告:需要(/db/connect.php):无法打开流:没有这样的文件或目录/home/sites/gorydev.net.ve/public_html/SistemaMatilcell/login.php 在第 17 行致命错误:require(): Failed opening required '/db/connect.php' (include_path='.:/usr/share/ pear55:/usr/share/php') 在 /home/sites/gorydev.net.ve/public_html/SistemaMatilcell/login.php 第 17 行 但是我确实上传了 /db/connect.php / 目录到我的主机服务器

标签: php database localhost hosting


【解决方案1】:

看起来您使用的是绝对路径,并且需要相对路径。

require '/db/connect.php';

应该是

require 'db/connect.php';

【讨论】:

  • 感谢@gre_gor 它确实解决了问题.. 但现在我输入管理员和密码 admin 并没有任何反应
猜你喜欢
  • 2021-10-20
  • 2018-09-16
  • 2021-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-24
相关资源
最近更新 更多