Query by table text using PDO

Asked

Viewed 80 times

0

My seemingly simple problem has given me headaches. I have a table that contains three columns: id, idpergunta, resposta. When a reply is sent to be inserted in this table I check if it already exists:

$where = [
            1 => $resposta['idpergunta'],
            2 => '' . $resposta['resposta'] . '',
        ];    

        return count($this->db->select("SELECT idresposta FROM resposta WHERE aberta = 1 AND idpergunta = ? AND resposta = ?", $where));

The method select is implemented using PDO as follows:

public function select($sql, $array = array(), $fetchMode = PDO::FETCH_OBJ, $class = '')
    {
        $stmt = $this->prepare($sql);
        foreach ($array as $key => $value) {
            if (is_int($value)) {
                $stmt->bindValue("$key", $value, PDO::PARAM_INT);
            } else {
                $stmt->bindValue("$key", $value);
            }
        }

        $stmt->execute();

        if ($fetchMode === PDO::FETCH_CLASS) {
            return $stmt->fetchAll($fetchMode, $class);
        } else {
            return $stmt->fetchAll($fetchMode);
        }
    }

However, the way it is, if the text of the answer is larger than two words it does not find it. Sometimes it works and sometimes it does not. I’ve tried several ways, but nothing solves it. Time works, time doesn’t work. What could be causing this? I am absolutely sure that the data is correct. If I do a query manually on the server the return happens normally.

  • How do you have three columns and on query has columns like idresposta and open? Being that it does not have in the description you gave of the table. I tested here and saw no error, I tested by taking all columns, without the aberta And with records of 2, 3, 4 and 5 words, maybe the error is there, but since we don’t have the code of the table we can not know. Take a look at what I did: http://github.com/Leonardo-Souza/MinPDO

  • I just forgot to mention the 'open' column is an integer that has a value of 0 or 1. Only.

No answers

Browser other questions tagged

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