Variable not defined in PHP Class

-1

I created a PHP class to update the data of a record in a table in the mysql database:

<?php

class InsertAccess{

public function insert_access($reg){

    require_once('../conn/conecta.php');

    $status = '1';

    try{
        $sql = "UPDATE 'tab_login' SET 'last_access' = :date_access, 'status' = :sts WHERE 'registro' = :reg";
        $access = $conecta->prepare($sql);
        $access->bindParam(":reg", $reg, PDO::PARAM_STR);
        $access->bindParam(":date_access", date('Y-m-d H:i:s'), PDO::PARAM_STR);
        $access->bindParam(":sts", $status, PDO::PARAM_STR);
        $access->execute();

    }catch (Exception $ex){
        echo 'ERRO: '.$ex;
    }
  } 
}
?>

You're giving me an undefined variable message:

Notice: Undefined variable: conecta in C:\wamp64\www\my_dir\actions\class_access.php on line 13

Can anyone tell me the correct way to use a php class to perform this type of action?

connecta.php

try {       
    $servidor = 'localhost';
    $usuario  = 'root';
    $senha    = '';
    $banco    = 'my_db';

    $conecta = new PDO("mysql:host=$servidor;dbname=$banco", $usuario , $senha, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
    $conecta->setAttribute ( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );  
} catch(PDOException $e) {
    echo 'ERRO DE CONEXÃO: ' . $e->getMessage();
}

Function call

$access_class = new InsertAccess();
$access_class->insert_access($item);

The InsertAccess class is a separate php file, and I'm using it in another php file by doing the instance as shown above.

    
asked by anonymous 22.05.2018 / 00:10

1 answer

0

Problem shown in question was solved using instead of require_once() require() :

class InsertAccess{

public function insert_access($reg){

require('../conn/conecta.php');

$status = '1';
$data_access = date('Y-m-d H:i:s');

try{
    $sql = "UPDATE 'tab_login' SET 'last_access' = :date_access, 'status' = :sts WHERE 'registro' = :reg";
    $access = $conecta->prepare($sql);
    $access->bindParam(":reg", $reg, PDO::PARAM_STR);
    $access->bindParam(":date_access", $data_access, PDO::PARAM_STR);
    $access->bindParam(":sts", $status, PDO::PARAM_STR);
    $access->execute();

}catch (Exception $ex){
    echo 'ERRO: '.$ex;
}
} 
}

Thank you all for the negative point in the matter and for the extremely gratifying help.

    
22.05.2018 / 23:11