Error when performing a database query

Asked

Viewed 3,146 times

4

I’m using Zend and 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;
  }
}

She begets me this beautiful and perfect consultation:

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')

but when executing 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 generated query is the same above. Running directly in the database it brings the data normally.

There was some detail I missed?

  • Strange this mistake. A question: id is sweep?

  • int and auto increment.

  • Have you tried to convert the $id for integer, just to see if it generates the query without the quotation marks? I understood that the query runs directly on the bench, but it’s just a kick that Zend can try to do some magic and it ends up hurting.

  • Before this query, you run another code next to the bank?

  • I’ve tried this tmbém @utluiz, I’m running nothing else @ricidleiv!

  • Can you enable the server log to monitor queries? Zend may be concatenating something in this query before sending it to the database. About the log, see this link, Help.

Show 1 more comment

3 answers

1

The $id parameter you pass is a String? If it is, do 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;
}
}
  • It was one of the first things I tried, it didn’t happen, but thank you...

0

Try it with that code

    $sql = $this->getDefaultAdapter()
        ->setIntegrityCheck(false)
        ->select()
        ->from($this->_name, $cols)
        ->where('id = ?', $id);
    return $this->fetchRow($sql);
  • the setIntegrityCheck(false) is for when you’ll make an appointment with join in other tables...anyway thanks for the intention.

-1

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

A tip, try running this query on the terminal or in a data modeling IDE like Mysql Workbench (Windows/Linux), Navicat(Windows/Linux) or Sequel Pro (OSX).

  • I spin it on Navicat, and it’s cool...

  • Using the same user that is being used in the application ?

  • Yes, @devsdmf...

Browser other questions tagged

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