Error in php in my syntax [closed]

-2
  

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 MySQL server version   for the right syntax to use near '' store_categories 'ORDER BY id DESC'

The codes below

<?php
class Site extends BD{
    public function getdate(){
        $data = getdate();
        $diaHoje = date('d');
        $mesgetdate = $date['mon'];
        /*$array_meses = array(1 => "Janeiro", 2 => "Fevereiro", 3 => "Março", 4 => "Abril", 5 => "Maio", 6 => "Junho", 
                               7 => "Julho", 8 => "Agosto", 9 => "Setembro", 10 => "Outubro", 11 => "Novembro", 12 => 'Dezembro');*/

        $meses = array (1 == "Janeiro", 2 => "Fevereiro", 3 => "Março", 4 => "Abril", 5 => "Maio", 6 => "Junho", 
                        7 => "Julho", 8 => "Agosto", 9 => "Setembro", 10 => "Outubro", 11 => "Novembro", 12 => "Dezembro");
        $horaAgora = date('H:i');
        $anoAtual = date('Y');

        return 'Hoje, '.$diaHoje.' de '.$meses[$mesgetdate].' de '.$anoAtual.' ás '.$horaAgora.'';
    }

    public function getMenu(){
        $img_cat = '<img src="'.PATH.'image/arrow.png"';
        $pega_categorias = "SELECT * FROM 'loja_categorias' ORDER BY id DESC";
        $executar = self::conn()->prepare($pega_categorias);
        $executar->execute();
        if($executar->rowCount() == 0){}else{
            while($categoria = $executar->fetchObject()){
                echo '<li>'.$img_cat.'<a href="'.PATH.'/categoria/'.$categoria->slug.'">'.$categoria->titulo.'';

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

?>'
    
asked by anonymous 18.12.2015 / 19:48

1 answer

3

Actually the error is mysql syntax:

SELECT * FROM 'loja_categorias' ORDER BY id DESC

In mysql we do not use apostrophes (single quotation marks) to select tables and columns where the '

Arrange by doing this:

SELECT * FROM 'loja_categorias' ORDER BY id DESC

And this:

SELECT * FROM 'loja_subcategorias' WHERE id_cat = ? 

We strongly recommend that you study the mysql documentation:

One more detail, here you wrote wrong $executar_sub->fechtObject( is not fechtObject correct fecthObject , correct it like this:

                echo '<ul>';
                    while($subcategoria = $executar_sub->fecthObject()){
                        echo '<li><a href="'.PATH.'/categoria/'.$categoria->slug.'/'.$sub_categoria->slug.'">'.$sub_categoria->titulo.'</a></li>';
                }
                echo '</ul></li>';

I recommend reading the documentation too:

18.12.2015 / 19:53