Help me with this error Object of class Pdostatement could not be converted to string in

Asked

Viewed 242 times

0

Inserting direct calling function. I have this error Catchable fatal error: Object of class Pdostatement could not be converted to string in C: wamp64 www Quest sql_geral.php on line 45

    $sql = new sql_geral();
$perFechadas = array(
    "Pergunta fechada 0",
    "Pergunta fechada 1",
    "Pergunta fechada 2",
    "Pergunta fechada 3"
);
$qntPerguntas = array(
    3,
    2,
    1,
    2
);
$respostas2 = array(
    array(
      'Resposta 0-1',
      'Resposta 0-2',
      'Resposta 0-3',
    ),
    array(
      'Resposta 1-1',
      'Resposta 1-2',
    ),
    array(
      'Resposta 2-1'
    ),
    array(
      'Resposta 3-1',
      'Resposta 3-2',
    ),
);
$perAbertas = array(
    "Pergunta Aberta 0",
    "Pergunta Aberta 1",
    "Pergunta Aberta 2",
    "Pergunta Aberta 3"
);

PHP function

$retorno = $sql->insertgeral('Nome Teste 1', "Descrição teste 1", "Categoria Teste 1", "Autor teste 1", date('Y-m-d H:i:s'), date('Y-m-d H:i:s'), $perFechadas, $perAbertas, $qntPerguntas, $respostas2);




public function insertgeral($nome_questionario, $descricao, $categoria, $autor, $datas, $data2, $perFechadas, $perAbertas, $qntPerguntas, $respostas2) {
    try {
        $this->con->beginTransaction();
        $stmt = "INSERT INTO
      questionario (myid, nome_questionario, descricao, categoria, data, data_update, autor)
      VALUES (NULL, :nome_questionario, :descricao, :categoria, :data, :data2, :autor);";
        $stmt = $this->con->prepare($stmt);
        $stmt->bindValue(":nome_questionario", $nome_questionario);
        $stmt->bindValue(":descricao", $descricao);
        $stmt->bindValue(":categoria", $categoria);
        $stmt->bindValue(":autor", $autor);
        $stmt->bindValue(":data", $datas);
        $stmt->bindValue(":data2", $data2);

        $stmt->execute();
        $id_Questionario = $this->con->lastInsertId();
        $i = 0;
        $sql = "";
        foreach ($perFechadas as $perguntaF) {
            $sql .="INSERT INTO perguntas (myid, id_questionario, texto_pergunta) values (NULL, :id_questionario, :texto_pergunta);";
            $sql = $this->con->prepare($sql);
            $sql->bindValue(":id_questionario", $id_Questionario);
            $sql->bindValue(":texto_pergunta", $perguntaF);
            $sql->execute();
            $idPerguntF = $this->con->lastInsertId();
            $sql_2 = "";
            for ($cont = 0; $cont < $qntPerguntas[$i]; $cont++) {
            $sql_2 .= "INSERT INTO respostas (myid, texto_resposta, id_perguntas) values (NULL, :texto_resposta, :id_perguntas);";
            $sql_2 = $this->con->prepare($sql_2);
            $sql_2->bindValue(":texto_resposta", $respostas2[$i][$cont]);
            $sql_2->bindValue(":id_perguntas", $idPerguntF);
            $sql_2->execute();    
            $i++;

            }

        }
        $sql_3 = "";
            foreach ($perAbertas as $perguntaA) {
            $sql_3 .= "insert into perguntas (myid, id_questionario, texto_pergunta) values (NULL, :id_Questionario, :perguntaA);";
            $sql_3 = $this->con->prepare($sql_3);
            $sql_3->bindValue(":texto_pergunta", $perguntaA);
            $sql_3->bindValue(":id_Questionario", $id_Questionario);
            //$sql_3->bindValue(":texto_pergunta", $perguntaA);
            $stmt->execute();
            }

        $this->con->commit();
        return true;
    } catch (Exception $f) {
        $log = fopen('loge.txt', 'a');
        fwrite($log, "ERRO EM ' -> instPesRast' -=- DIA " . date("d/m/Y") . " -=- HORA " . date("H:m:s") . "\r\n" . $f->getMessage() . "\r\n");
        fclose($log);
        $this->con->rollBack();
        return false;
    }
}
  • look at this line 45 and show us what it is, but basically you’re trying to use an object as a string

  • 1

    I’m not sure but I believe the problem is when you do $sql .= ... inside the loop, the first time everything normal but the second you try to concatenate the $sql = $this->con->prepare($sql); from the previous loop with the sql string of the current loop

  • OK. That’s what I got.

No answers

Browser other questions tagged

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