Problem when changing using Pdo

Asked

Viewed 77 times

-1

I’m developing a system for college and I’m having the following problem

Notice: Undefined property: stdClass::$Disciplina in C:\Users\lucas.vianna\Desktop\SGA\db\turmaDAO.php on line 64

Notice: Undefined variable: getDisciplina in C:\Users\lucas.vianna\Desktop\SGA\turmas.php on line 41

Fatal error: Uncaught Error: Method name must be a string in C:\Users\lucas.vianna\Desktop\SGA\turmas.php:41 Stack trace: #0 {main} thrown in C:\Users\lucas.vianna\Desktop\SGA\turmas.php on line 41

Line 41: $disciplina = $resultado->$getDisciplina();
Line 64: $turma->setDisciplina($rs->Disciplina);


Follow the update code on DAO:

     public function atualizar($turma){
        global $pdo;
        try {
            $statement = $pdo->prepare("SELECT idTurma, Disciplina_idDisciplina,Nome FROM turma WHERE idTurma = :id");
            $statement->bindValue(":id", $turma->getIdTurma());
            if ($statement->execute()) {
                $rs = $statement->fetch(PDO::FETCH_OBJ);
                $turma->setIdTurma($rs->idTurma);
                $turma->setDisciplina($rs->Disciplina);
                $turma->setNome($rs->Nome);
                return $turma;
            } else {
                throw new PDOException("Erro: Não foi possível executar a declaração sql");
            }
        } catch (PDOException $erro) {
            return "Erro: ".$erro->getMessage();
        }
    }

Follow the select code in the view

if (isset($_REQUEST["act"]) && $_REQUEST["act"] == "upd" && $id != "") {

    $turma = new turma($id, '','');

    $resultado = $object->atualizar($turma);
    $nome = $resultado->getNome();
    $disciplina = $resultado->$getDisciplina();
}



<select name="disciplina"><?php
    $query = "SELECT * FROM Disciplina order by Nome;";
    $statement = $pdo->prepare($query);
    if ($statement->execute()) {
        $result = $statement->fetchAll(PDO::FETCH_OBJ);
        foreach ($result as $rs) {
            if ($rs->idDisciplina == $disciplina) {
                echo "<option value='$rs->idDisciplina' selected>$rs->Sigla</option>";
            } else {
                echo "<option value='$rs->idDisciplina'>$rs->Sigla</option>";
            }
        }
    } else {
        throw new PDOException("Erro: Não foi possível executar a declaração sql");
    }
    ?>
</select>
  • The estate Discipline on the line $rs->Disciplina does not exist. The correct is $rs->Disciplina_idDisciplina

  • And you don’t need the dollar $ to call a method, except if you use a variable with the method name, for example: $getDisciplina = "getDisciplina"; $resultado->$getDisciplina()

  • Worked thanks!

1 answer

0

Notice: Undefined Property: stdClass::$Discipline : Means that the Class is not defined; Notice: Undefined variable: getDiscipline : Derived from the first error, obviously if the class is not defined the methods also not; Fatal error: Uncaught Error: Method name must be a string : You are probably creating a function with an invalid name with special characters or number: PHP Function Names: Never start with special characters or numbers, examples: "{$, #, %, 1}".

And regarding the global $Pdo it is not a good practice to have a class with the connection to the data bannco in a global way.

I consider the creation of a Singleton class:

class DBConnection{
   private static $instance;
   private function __constructor(){}
   private function __clone(){}

   public function getDBaseConnection(){
      if(!isset(static::$instance)){
          $pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
          static::$instance = new PDO(paramters, $pdo_options);
      }

      return static::$instance;
   }

}

$conneciton  = DBConnection::getDBaseConnection();
$connection->query(param);

//It will be the same class to be used in the whole project and only need to be instantiated; If you want something more flexible like using another data white you have the option to pass the database name as parameter for the getDBaseConnection($dbname method);

  • The explanations in the first paragraph are completely wrong and your code is incorrect.

  • The idea was to help. And what is the error in the code?

  • The error was corrected, it was in the word static. I understand the idea is to help.

Browser other questions tagged

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