Why am I unable to fetch any data from a mysql table

Asked

Viewed 25 times

-1

what error there is in Foreach?

<?php
    $banco = "mysql:dbnmae=banco;host=localhost";
    $user = "root";
    $senha = "";
    try{
       $conn= new PDO($banco,$user,$senha);
       echo "<h1>conectado com sucesso</h1>";
       $sql = " SELECT * FROM banco";
       $linha =$conn->query($sql);

       foreach($linha as $linhas){
           echo $linhas['nome'];
       }
    }
    catch (PDOException $e){
        echo "<h1>Falha ao conectar</h1>";

       $erro = $e->getCode();
      echo $erro;
    }
  • 1

    You wrote dbnmae instead of dbname

  • you saved my life, very sheltered

1 answer

1


It’s a clerical error. You wrote dbnmae instead of dbname. Change your variable $banco for:

$banco = "mysql:dbname=banco;host=localhost";

Browser other questions tagged

You are not signed in. Login or sign up in order to post.