Error performing a database query

4

I'm using Zend and I have the following function:

public function getChamado($id) {
  try {
    $cols = array(
      'id', 'titulo', 'descricao', 'fk_status', 'fk_local',
      'fk_tipo', 'created', 'modified', 'finished', 'fk_usuario',
      'fk_restricao', 'fk_prioridade'
    );

    $sql = $this->getDefaultAdapter()
      ->select()
      ->from($this->_name, $cols)
      ->where('id = ?', $id);

    return $this->fetchRow($sql);

  } catch(Exception $e){
    echo $e->getMessage(); die;
  }
}

It generates this beautiful and perfect query:

SELECT
    'tbl_chamado'.'id',
    'tbl_chamado'.'titulo',
    'tbl_chamado'.'descricao',
    'tbl_chamado'.'fk_status',
    'tbl_chamado'.'fk_local',
    'tbl_chamado'.'fk_tipo',
    'tbl_chamado'.'created',
    'tbl_chamado'.'modified',
    'tbl_chamado'.'finished',
    'tbl_chamado'.'fk_usuario',
    'tbl_chamado'.'fk_restricao',
    'tbl_chamado'.'fk_prioridade'
FROM
    'tbl_chamado'
WHERE
    (id = '1')

however when running it gives the following error:

  

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 '' at line 1

All variables are correct, the query generated is the same as above. Running directly on the bank it carries the data normally.

Was there a detail I let pass?

    
asked by anonymous 31.01.2014 / 00:09

3 answers

1

Is the $ id parameter you passed a String? If so, try the following test.

public function getChamado($id) {
try {
$id = (int) $id;
$cols = array(
  'id', 'titulo', 'descricao', 'fk_status', 'fk_local',
  'fk_tipo', 'created', 'modified', 'finished', 'fk_usuario',
  'fk_restricao', 'fk_prioridade'
);

$sql = $this->getDefaultAdapter()
  ->select()
  ->from($this->_name, $cols)
  ->where('id = ?', $id);

return $this->fetchRow($sql);

} catch(Exception $e){
echo $e->getMessage(); die;
}
}
    
06.02.2014 / 21:24
0

Try this code

    $sql = $this->getDefaultAdapter()
        ->setIntegrityCheck(false)
        ->select()
        ->from($this->_name, $cols)
        ->where('id = ?', $id);
    return $this->fetchRow($sql);
    
31.01.2014 / 00:57
-1

By mistake there is something wrong in writing some field or table in the Query or the user you are using is not allowed to perform this action on this database or table.

A tip, try to run this query on the terminal or in a data modeling IDE such as MySQL Workbench (Windows / Linux), NaviCat (Windows / Linux) or Sequel Pro (OSX).

    
31.01.2014 / 00:15