I DO NOT GET ANY MYSQL REGISTER IN PHP

2

I've been working with php for some time and trying to make the MYSQL code work inside PHP, I'm working with PDO but at the moment I want to get the records from my database and put the print_r ($ variable); shows me an empty array.

<?php 

function conexion(){
    try {
        $conexion = new PDO('mysql:localhost;dbname=u766373438','u766373438user', '1987');
        return $conexion;
    } catch(PDOException $e) {
        return false;
    }
}

$sql = conexion();

$table = $sql->prepare('SELECT * FROM ejemplo');
$table->execute();
$resultado = $table->fetchAll();

print_r($resultado);

? >

    
asked by Ecarv 09.08.2018 в 18:07
source

2 answers

2

According to the official documentation, your connection syntax lacks some arguments.

link

Your version

$conexion = new PDO('mysql:localhost;dbname=u766373438','u766373438user', '1987');

Version of the documentation, you have to define the word host.

$conexion = new PDO('mysql:host=localhost;dbname=u766373438', 'u766373438user', '1987');
    
answered by 09.08.2018 / 19:29
source
0

What you have done is fine, but this line also comes.

 $conexion = new PDO('mysql:localhost;dbname=u766373438','u766373438user', '1987');

your mistake in using the same name as your function, I ran the code ok

<?php 
function conexion(){
    try {
        $db = new PDO('mysql:localhost;dbname=u766373438','u766373438user', '1987');        
        return $db;
    } catch(PDOException $e) {
        return false;
    }
}

$sql = conexion();
$table = $sql->prepare('SELECT * FROM ejemplo');
$table->execute();
$resultado = $table->fetchAll();
print_r($resultado);
?>

As I always do my tests

    
answered by 09.08.2018 в 20:41