简体   繁体   English

连接到MySQL数据库

[英]Connection to mysql db

I want to connect to my db using PDO but I can't. 我想使用PDO连接到我的数据库,但不能。 I have tested the connection in another hosting and working without problems but I had that changed the hosting, I changed the parameters to connect but it doesn't work 我已经在另一台主机上测试了连接并且可以正常工作,但是我更改了主机,更改了要连接的参数,但是它不起作用

I use a .ini file that has the data to connect to my db. 我使用一个.ini文件,该文件具有连接到数据库的数据。 Something like that: 像这样:

[database]
username = ....
password = *******
dbname = dbname...
driver = mysql
host = example.net


<?php
class Conexion extends PDO {
    private $driver;
    private $host;
    private $nombre_bd;
    private $usuario;
    private $password;

    public function __construct() {
        $config = parse_ini_file('../admin/cfg/config_pdo.ini');

        $this->usuario = $config['username'];
        $this->password = $config['password'];
        $this->nombre_bd = $config['dbname'];
        $this->driver = $config['driver'];
        $this->host = $config['host'];

        try {
            parent::__construct($this->driver.':host='.$this->host.';dbname='.$this->nombre_bd,$this->usuario,$this->password);

        } catch (PDOException $e) {
            echo 'Ha surgido un error al conectarse a la base de datos. ' . '</br>' . 'Detalle: ' . $e->getMessage();
            exit;
        }
    }
}

The result is "Could not find driver" I checked if pdo is enabled using info() in php file and everything is correct. 结果是“找不到驱动程序”,我检查了是否使用php文件中的info()启用了pdo,并且一切正确。

Thanks so much, best regards 非常感谢,最好的问候

$this->driver.':host='.$this->host.';dbname='.

after driver.' :host driver.' :host之后driver.' :host driver.' :host

you have " : " 你有“ :

but after host.'; 但在host.';之后host.';

you have " ; " 你有“ ;

check whether it should be " : " or " ; " 检查它应该是“ : ”还是“ ;

I think that the problem is the hosting web. 我认为问题出在托管网站。 I created a new connection using mysqli and the result is: Error: No se pudo conectar a MySQL. 我使用mysqli创建了一个新连接,结果是:错误:没有伪连接MySQL。 errno de depuración: 2005 error de depuración: Unknown MySQL server host 'mysql55.servidoraweb.net' (0) 错误的消息:2005错误的错误的消息:未知的MySQL服务器主机“ mysql55.servidoraweb.net”(0)

<?php
$enlace = mysqli_connect("host", "user", "pass", "db");

if (!$enlace) {
    echo "Error: No se pudo conectar a MySQL." . PHP_EOL;
    echo "errno de depuración: " . mysqli_connect_errno() . PHP_EOL;
    echo "error de depuración: " . mysqli_connect_error() . PHP_EOL;
    exit;
}

echo "Éxito: Se realizó una conexión apropiada a MySQL! La base de datos mi_bd es genial." . PHP_EOL;
echo "Información del host: " . mysqli_get_host_info($enlace) . PHP_EOL;

mysqli_close($enlace);
?>

What do you think? 你怎么看? Thank you 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM