PHP code error - SQLSTATE[42000]

Asked

Viewed 619 times

-1

I am trying to list the categories and subcategories present in the database but the following error is pointed out:

Fatal error: Uncaught Exception 'Pdoexception' with message 'SQLSTATE[42000]: Syntax error or access Violation: 1064 You have an error in your SQL syntax; check the manual that Corresponds to your Mariadb server version for the right syntax to use near 'loja_categories' ORDER BY id DESC' at line 1' in C: xampp htdocs lojacurso classes Site.class.php:8 Stack trace: #0 C: xampp htdocs lojacurso classes Site.class.php(8): Pdostatement->execute() #1 C: xampp htdocs lojacurso pages categoria.php(13): Site->getMenu() #2 C: xampp htdocs lojacurso index.php(20): include_once('C: xampp htdocs...') #3 {main} thrown in C: xampp htdocs lojacurso classes Site.class.php on line 8

The code is this:

<?php

class Site extends BD{

    public function getMenu(){
        $pegar_categorias = "SELECT * FROM 'loja_categorias' ORDER BY id DESC";
        $executar = self::conn()->prepare($pegar_categorias);
        $executar->execute();

        if ($executar->rowCont() == 0) {
            # não faz nada pois não há categorias
        }else{
            while ($categoria = $executar->fetchObject()) {
                echo '<li><a href="'.PATH.'/categoria/'.$categoria->slug.'">'.$categoria->titulo.'';

                    $pegar_subcategorias = "SELECT * FROM 'loja_subcategorias' WHERE id_cat = ?";
                    $executar_sub = self::conn()->prepare($pegar_subcategorias);
                    $executar_sub->execute(array($categoria->id));
                    if ($executar_sub->rowCont() == 0) {
                        echo '</li>';
                    }else{
                        echo '<ul>';
                        while ($subcategoria == $executar_sub->fetchObject()) {
                            echo '<li><a href=" '.PATH.'/categoria/'.$categoria->slug.'/'.$subcategoria->slug.' ">'.$subcategoria->titulo.'</a></li>';
                        }//termina while subcategoria
                        echo '</ul></li>';

                    }//termina else dos resultados da subcategoria
            }//termina while das categorias
        }//primeiro else
    }//TERMINA FUNÇÃO getMenu

}

?>

1 answer

1

Remove the simple quotes from your SELECT, being like this:

$pegar_categorias = "SELECT * FROM loja_categorias ORDER BY id DESC";

Another alternative is to replace single quotes with crase:

$pegar_categorias = "SELECT * FROM `loja_categorias` ORDER BY id DESC";
  • When shooting points out the following error: Fatal error: Call to Undefined method Pdostatement::rowCont() in C: path classes Site.class.php on line 10"

  • I saw here the spelling error :D

  • "Count" and not "cont" as it was

Browser other questions tagged

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