Solucionado (ver solução)
Solucionado
(ver solução)
9
respostas

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax

Olá. Bom dia.

Estou com um problema no curso de laravel. estou na aula 4, no exercício onde criamos a view "detalhes.php" fiz o exercício até a parte onde devemos realizar a consulta passando o valor 1 como $id, ou seja meu código ainda não filtra os produtos, mas ele deveria sempre retornar o produto com o id 1. O problema é que quando fui testar recebi uma mensagem de erro de sintaxe SQL. Dei uma olhada na minha sintaxe mas não percebi qual o erro. Segue código da view e do produto controller:

detalhes.php:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="/css/app.css">
    <title>Detalhes do Produto</title>
  </head>
  <body>
      <h1>Detalhes do Produto: <?php $p->nome?></h1>
      <ul>
          <li>
              <b>Valor:</b> R$ <?php $p->valor ?>
          </li>
          <li>
              <b>Descrição:</b> <?php $p->descricao ?>
          </li>
          <li>
              <b>Quantidade em estoque:</b> R$ <?php $p->quantidade ?>
          </li>
      </ul>
  </body>
</html>

ProdutoController.php:

<?php namespace estoque\Http\Controllers;

use Illuminate\Support\Facades\DB;

class ProdutoController extends Controller{
    public function lista(){
        $produtos = DB::select('select * from produtos');
        return view('listagem')->with('produtos', $produtos);
    }

    public function mostra(){
        $id=1;
        $resposta = DB::select('select * from produtos where id = ?' [$id]);
        return view('detalhes')->with('p', $resposta);
    }

}


 ?>
9 respostas

Falta uma vírgula para separar os argumentos da função:

 $resposta = DB::select('select * from produtos where id = ?', [$id]);

Opa. Obrigado Gabriel. Corrigi isso e recebi outro erro:

ErrorException in detalhes.php line 9: Trying to get property of non-object

O que tem na linha 9 ?

O erro indica que alguma "propriedade" não existe para o objeto. Os nomes das propriedades estão corretos (nome, quantidade, valor e descricao)?

Vi também que falta um espaço nesta linha:

<?php $p->nome?>

O correto é:

<?php $p->nome? >

Essa é a linha nove:

      <h1>Detalhes do Produto: <?php $p->nome ?> </h1>

Corrigi aqui a questão do espaço mas o erro não mudou. Chequei no BD e os nomes estão corretos.

O erro é a ausência do echo.

Pode fazer assim:

<h1>Detalhes do Produto: <?php echo $p->nome ?></h1>

ou assim:

<h1>Detalhes do Produto: <?= $p->nome ?></h1>

Corrigi o código mas continua sendo exibido o mesmo erro:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="/css/app.css">
    <title>Detalhes do Produto</title>
  </head>
  <body>
      <h1>Detalhes do Produto: <?= $p->nome ?> </h1>
      <ul>
          <li>
              <b>Valor:</b> R$ <?= $p->valor ?>
          </li>
          <li>
              <b>Descrição:</b> <?= $p->descricao ?>
          </li>
          <li>
              <b>Quantidade em estoque:</b> R$ <?= $p->quantidade ?>
          </li>
      </ul>
  </body>
</html>

Segue erro na integra:


Whoops, looks like something went wrong.
1/1 ErrorException in detalhes.php line 9: Trying to get property of non-object

    in detalhes.php line 9
    at HandleExceptions->handleError(8, 'Trying to get property of non-object', 'C:\\xampp\\htdocs\\estoque\\resources\\views\\detalhes.php', 9, array('__path' => 'C:\\xampp\\htdocs\\estoque\\resources\\views/detalhes.php', '__data' => array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'p' => array(object(stdClass))), 'obLevel' => 1, '__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'p' => array(object(stdClass)))) in detalhes.php line 9
    at include('C:\\xampp\\htdocs\\estoque\\resources\\views\\detalhes.php') in PhpEngine.php line 42
    at PhpEngine->evaluatePath('C:\\xampp\\htdocs\\estoque\\resources\\views/detalhes.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'p' => array(object(stdClass)))) in PhpEngine.php line 20
    at PhpEngine->get('C:\\xampp\\htdocs\\estoque\\resources\\views/detalhes.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'p' => array(object(stdClass)))) in View.php line 137
    at View->getContents() in View.php line 120
    at View->renderContents() in View.php line 85
    at View->render() in Response.php line 38
    at Response->setContent(object(View)) in Response.php line 225
    at Response->__construct(object(View)) in Router.php line 602
    at Router->prepareResponse(object(Request), object(View)) in Router.php line 560
    at Router->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 30
    at Pipeline->Illuminate\Routing\{closure}(object(Request)) in SubstituteBindings.php line 41
    at SubstituteBindings->handle(object(Request), object(Closure)) in Pipeline.php line 148
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
    at Pipeline->Illuminate\Routing\{closure}(object(Request)) in VerifyCsrfToken.php line 65
    at VerifyCsrfToken->handle(object(Request), object(Closure)) in Pipeline.php line 148
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
    at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
    at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 148
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
    at Pipeline->Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 64
    at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 148
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
    at Pipeline->Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
    at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 148
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
    at Pipeline->Illuminate\Routing\{closure}(object(Request)) in EncryptCookies.php line 59
    at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 148
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
    at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 102
    at Pipeline->then(object(Closure)) in Router.php line 561
    at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 520
    at Router->dispatchToRoute(object(Request)) in Router.php line 498
    at Router->dispatch(object(Request)) in Kernel.php line 174
    at Kernel->Illuminate\Foundation\Http\{closure}(object(Request)) in Pipeline.php line 30
    at Pipeline->Illuminate\Routing\{closure}(object(Request)) in TransformsRequest.php line 30
    at TransformsRequest->handle(object(Request), object(Closure)) in Pipeline.php line 148
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
    at Pipeline->Illuminate\Routing\{closure}(object(Request)) in TransformsRequest.php line 30
    at TransformsRequest->handle(object(Request), object(Closure)) in Pipeline.php line 148
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
    at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ValidatePostSize.php line 27
    at ValidatePostSize->handle(object(Request), object(Closure)) in Pipeline.php line 148
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
    at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 46
    at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 148
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
    at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 102
    at Pipeline->then(object(Closure)) in Kernel.php line 149
    at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 116
    at Kernel->handle(object(Request)) in index.php line 54
    at require_once('C:\\xampp\\htdocs\\estoque\\public\\index.php') in server.php line 21
solução!

Altere a função para

 public function mostra(){
      $id=1;
      $resposta = DB::select('select * from produtos where id = ?', [$id]);
      return view('detalhes')->with('p', $resposta[0]);
    }

Isto deve ser feito porque somente um elemento será retornado na forma de array.

Hum... Obrigado Gabriel .

Deu certo.