dongming0505 2016-04-04 21:39
浏览 72
已采纳

无法用php连接到主机上的数据库

Hello guys I'm developing a small web application with PHP in which I can log in and add some things to the DB, everything works fine in my localhost machine so I upload it to my hosting server which have the same dbname, dbuser, dbpassword than my localhost.. but when I hit the login button with the username and password I get this error:

Error after click login button in hosting server

and I know the hosting is up and running because when I go to the web url:

gorydev.net.ve/SistemaMatilcell

this is the code for 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;
    }
}

this is the connect.php file:

<?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();
}   
?>

the config.php file:

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

and my Config.php class:

<?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;
    }
}
?>

and my DB in my hosting looks like this: Hosting Database

with error reporting:

Warning: require(/db/connect.php): failed to open stream: No such file or directory in /home/sites/gorydev.net.ve/public_html/SistemaMatilcell/login.php on line 17 Fatal error: require(): Failed opening required '/db/connect.php' (include_path='.:/usr/share/pear55:/usr/share/php') in /home/sites/gorydev.net.ve/public_html/SistemaMatilcell/login.php on line 17 /db/connect.php/

I repeat in my localhost pc it works fine.. same dbname same username and password

  • 写回答

1条回答 默认 最新

  • drrrdo0802 2016-04-04 22:36
    关注

    Looks like that you are using an absolute path and you need a relative one.

    require '/db/connect.php';
    

    should probably be

    require 'db/connect.php';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 5037端口被adb自己占了
  • ¥15 Error in check.length("fill") : 'gpar'成分'fill'的长度不能为零
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误