无法连接到主机中的数据库 php
Impossible to connect to database in host with php
大家好,我正在使用 PHP 开发一个小型 Web 应用程序,我可以在其中登录并向数据库添加一些内容,在我的本地主机上一切正常
所以我将它上传到我的托管服务器,它与我的本地主机具有相同的 dbname、dbuser、dbpassword .. 但是当我使用用户名和密码点击登录按钮时
我收到此错误:
而且我知道主机已经启动 运行 因为当我上网时 url:
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 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;
}
}
?>
我的主机中的数据库如下所示:
有错误报告:
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/
我在我的本地主机电脑上重复它工作正常..相同的数据库名相同的用户名和密码
看来您使用的是绝对路径,您需要一个相对路径。
require '/db/connect.php';
应该是
require 'db/connect.php';
大家好,我正在使用 PHP 开发一个小型 Web 应用程序,我可以在其中登录并向数据库添加一些内容,在我的本地主机上一切正常 所以我将它上传到我的托管服务器,它与我的本地主机具有相同的 dbname、dbuser、dbpassword .. 但是当我使用用户名和密码点击登录按钮时 我收到此错误:
而且我知道主机已经启动 运行 因为当我上网时 url:
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 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;
}
}
?>
我的主机中的数据库如下所示:
有错误报告:
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/
我在我的本地主机电脑上重复它工作正常..相同的数据库名相同的用户名和密码
看来您使用的是绝对路径,您需要一个相对路径。
require '/db/connect.php';
应该是
require 'db/connect.php';