Solucionado (ver solução)
Solucionado
(ver solução)
1
resposta

FATAL ERROR - não consigo resolver

Já li os tópicos anteriores do fórum, mas não consegui solucionar esse problema. Peço ajuda aos universitários!

Fatal error: Uncaught ArgumentCountError: Too few arguments to function Artigo::construct(), 0 passed in C:\xampp\htdocs\blog\index.php on line 6 and exactly 1 expected in C:\xampp\htdocs\blog\Artigo.php:6 Stack trace: #0 C:\xampp\htdocs\blog\index.php(6): Artigo->construct() #1 {main} thrown in C:\xampp\htdocs\blog\Artigo.php on line 6

// Artigo.php
<?php

class Artigo
{
    private $mysql;
    public function __construct(mysqli $mysql)
    {
            $this->mysql = $mysql;
    }

    public function exibirTodos(): array
    {
        $resultado = $this->mysql->query('SELECT id, titulo, conteudo FROM artigos');
        $artigos = $resultado->fetch_all(MYSQLI_ASSOC);

        return $artigos;
    }
}
// config.php
<?php

$mysql = new mysqli('localhost','root', '', 'blog');
$mysql->set_charset('utf8');

if ($mysql == FALSE)  {
    echo "ERRO NA CONEXÃO AO BD";
}
// index.php
<?php

require 'config.php';

include 'Artigo.php';
$artigo = new Artigo();
$artigos = $artigo->exibirTodos();

?>
<!DOCTYPE html>
<html lang="pt-br">

<head>
    <title>Meu Blog</title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="style.css">
</head>

<body>
    <div id="container">
        <h1>Meu Blog</h1>
        <?php foreach ($artigos as $artigo) : ?>
        <h2>
        <a href="artigo.php?id=<?php echo $artigo['id']; ?>">
                <?php echo $artigo['titulo']; ?>
            </a>
        </h2>
        <p>
            <?php echo $artigo['conteudo']; ?>
        </p>
        <?php endforeach; ?>
    </div>
</body>

</html>
1 resposta
solução!

Consegui corrigir o erro assistindo a aula 2.5 - Exibindo na página. o erro estava na linha 6 => $artigo = new Artigo(); foi necessário inserir ($mysql ) e tudo funcionou normalmente.

<?php

require 'config.php';

include 'Artigo.php';
$artigo = new Artigo($mysql);
$artigos = $artigo->exibirTodos();

?>
<!DOCTYPE html>
<html lang="pt-br">

<head>
    <title>Meu Blog</title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="style.css">
</head>

<body>
    <div id="container">
        <h1>Meu Blog</h1>
        <?php foreach ($artigos as $artigo) : ?>
        <h2>
        <a href="artigo.php?id=<?php echo $artigo['id']; ?>">
                <?php echo $artigo['titulo']; ?>
            </a>
        </h2>
        <p>
            <?php echo $artigo['conteudo']; ?>
        </p>
        <?php endforeach; ?>
    </div>
</body>

</html>

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software