Error in php in my syntax

Asked

Viewed 85 times

-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 '''loja_categorias' ORDER BY id DESC'

The codes at the bottom

<?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>';
                }
            }
        }
    }
}

?>`
  • 1

    What do you really want to do? Be more specific in your question so the experts can help, I recommend that after you do this follow the tour of Sopt http://answall.com/tour

1 answer

4


Actually the error is mysql syntax:

SELECT * FROM 'loja_categorias' ORDER BY id DESC

In mysql we do not use apostrophes (single quotes) to select tables and columns where the sign of ` should be (severe accent or backtick)

Fix it by doing this:

SELECT * FROM `loja_categorias` ORDER BY id DESC

And this:

SELECT * FROM `loja_subcategorias` WHERE id_cat = ? 

It is highly recommended to study mysql documentation:


One more detail, here you wrote wrong $executar_sub->fechtObject( is not fechtObject the correct is fecthObject, correct him 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:

Browser other questions tagged

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