Fatal error: Uncaught Error: Call to a Member Function prepare() on null

Asked

Viewed 798 times

1

The site presents this error when connecting with BD.

Someone can help me?

Fatal error: Uncaught Error: Call to a Member Function prepare() on null in /home/clinicadrbalieiro/www/bariatrica/index.php:20 Stack trace: #0 {main} thrown in /home/xxxx/www/bariatrica/index.php on line 20

<?php
   date_default_timezone_set('America/Sao_Paulo');
   define('HOST','localhost');
   define('DB','xxxxx');
   define('USER','xxxx');
   define('PASS','xxxxx');

try {
    $pdo = new PDO('mysql:host='.HOST.'; dbname='.DB,USER,PASS, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(Exception $e) {
    echo 'Erro ao conectar';
}
?>

It directs the error to line 20 of the index.php file

<?php
$sql = $pdo->prepare("SELECT * FROM `banner`");
$sql->execute();
$clinicadrbalie = $sql->fetchAll();
?>
    <div class="slider">
        <ul class="slides">
            <?php foreach ($clinicadrbalie as $key => $value): ?>
                <li>
                    <?php echo '<img src="data:image/jpg;base64,'.base64_encode( $value['imagem'] ).'"/>';  ?>
                    <!-- <div class="caption center-align">
                        <h3><?php// echo $value['titulo']; ?></h3>
                        <h5 class="light grey-text text-lighten-3"><?php //echo $value[descricao]; ?></h5>
                    </div> -->
                </li>
            <?php endforeach; ?>
        </ul>
        <div class="caption left-align">
            <h2>Qualidade de vida <br>acima de tudo!</h2>
            <h5>| Cirurgia Bariátrica e Metabólica | Cirurgia Robótica |</h5>
        </div>
    </div>
  • For the description of your mistake $pdo is null and why it is not finding prepare() on the line $sql = $pdo->prepare("SELECT * FROM banner");. I believe you’re missing the include or require of your connection file with the database that defines the $pdo

No answers

Browser other questions tagged

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