Error in sentence usanto query ()

2

I'm doing an internal query and I want to get a field ( idperiodo which is integer ), there should only be one record with the field Active that's why I use limit 1 . I have this code:

public function buscar_campo() {
    try {
            $sql = "SELECT idperiodo FROM periodo WHERE estado = 'A' limit 1";

            $BD = new Con();
            $resultado = $BD->query($sql);

            $fila = $resultado->fetchColum();
            $registroVO = new periodoVO();
            $registroVO->setPeriodo( $fila['periodo'] );
            return $registroVO;

        } 
}

The error is on line $fila = $resultado->fetchColum();

Sample:

  

Fatal error: Uncaught Error: Call to undefined method   PDOStatement :: fetchColum ()

    
asked by Piropeator 06.05.2018 в 17:37
source

1 answer

2

You have a syntax error: fetchColum , in fact the method is: fetchColumn .

But read carefully the documentation and examples , because this method does not return a associative array of results , therefore here you will have an Undefined index , because fetchColum returns the value of that column in clean:

$registroVO->setPeriodo( $fila['periodo'] );

Write your function like this:

public function buscar_campo() {
    try {
            $sql = "SELECT idperiodo FROM periodo WHERE estado = 'A' limit 1";

            $BD = new Con();
            $resultado = $BD->query($sql);

            $fila = $resultado->fetchColumn();
            $registroVO = new periodoVO();
            $registroVO->setPeriodo( $fila );
            return $registroVO;

        } 

    //Asumo que omitiste el catch por motivos de brevedad
}
    
answered by 06.05.2018 / 17:45
source