Warning: PDO :: __ construct () expects parameter 4 to be array, string given in

0

I'm starting to program in PHP and MySql and trying to connect the database with the PDO method marks me the error, This is my code:

else {
        try {
            $conexion  = new PDO('mysql:host=localhost;dbname=registro_prueba1', 'root', 'contraseña', '');
        } catch (PDOException $e) {
            echo "Error " . $e->getMessage();

The error that is presented is the following:

  

Warning: PDO :: __ construct () expects parameter 4 to be array, string   given in

Does anyone know how to do it?

    
asked by Mike Reyes 17.05.2017 в 17:52
source

2 answers

0

What happens is that the last parameter of the function PDO::__construct() receives an array of options of type array(key=>value) and you are sending an empty string when putting your two quotes '' .

Simply omit it since it is optional, so that your construction function is as follows:

$conexion = new PDO('mysql:host=localhost;dbname=registro_prueba1', 'root', 'contraseña'); 
    
answered by 17.05.2017 в 17:56
0

I suppose you try to make a persistent connection with PDO PDO::ATTR_PERSISTENT , but you are sending an empty string due to your ' ' and that last parameter supports only an array array( PDO::ATTR_PERSISTENT => true

You can only delete these simple quotes ' ' after password, leaving your connection variable then:

$conexion = new PDO('mysql:host=localhost;dbname=registro_prueba1', 'root', 'contraseña');

With that you will connect in the same way with your database

    
answered by 24.06.2018 в 02:10