2

I made this simple database function and a simple user class, but i can't implement the database connection inside the class.

dbconfig.php

<?php
session_start();


$db_user="root";
$db_pass="";

try {
  $con = new PDO('mysql:host=localhost;dbname=hrm', $db_user, $db_pass);
  $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}

catch(PDOException $e)
{
echo $e->getMessage();
}


?>

This is the class:

 <?php

 class user
{
   private $db;

   function __construct($con){
      include_once '..\dbconfig.php';
      $this->db=$con;
    }

   function listargeral($departamento){
      try{
       $stmt=$this->db->prepare("SELECT * from users where departamento=:departamento");
    $stmt->execute(array(':departamento'=>$departamento));
    while($userRow=$stmt->fetch(PDO::FETCH_ASSOC)){
            echo ' <li data-jstree=\'{ "icon" : "fa fa-user" }\'>';
            echo $userRow['nome']." - ".$userRow['acesso'];
            echo '</li>';
    }
} Catch(PDOException $e){
    echo $e->getMessage();}
}
   function logado(){
      if(isset($_SESSION['sessao'])){
         return true;
     }
  }

   function entrar($username, $password){
      try{
            $stmt=$this->db->prepare("SELECT * from users where 
            user_name=:username LIMIT 1");
            $stmt->execute(array(':username'=>$username));
            $userRow=$stmt->fetch(PDO::FETCH_ASSOC);
            if($stmt->rowcount()>0){
                 $_SESSION['sessao']=$userRow['userid'];
                 return true;
          }
     }

     catch(PDOException $e){
        echo $e->getMessage();
      }
     }

There is any alternative? Thank you guys, i'm still learning.

UPDATE, header page for example:

<!doctype html>


<?php

require_once 'dbconfig.php';
include_once 'Class/class.user.php';
$user= new user($con);


// VERIFICAR SE ESTÁ LOGADO
 if($user -> logado()==""){
   $user->redirect('login.php');

}
Cœur
  • 37,241
  • 25
  • 195
  • 267
sygamers
  • 57
  • 1
  • 1
  • 8

1 Answers1

0

Firstly you are including the dbconfig.php file twice, once in your main index page and once in your class. This is not necessary.

The mistake you are making is a scoping mistake. You are assuming that your variable $con as set in your dbconfig.php file is available outside of that file, which it is not.

There are two ways to make it available with the scope of your class file:

  1. return $con; <-- add this to the end of your dbconfig.php file.
  2. Change $con into a global variable with two steps:-

    A) declare it as global in dbconfig.php like so global $con;
    B) declare it again in your class file at the top: global $con

Either should work.

E.g. 1 – Using global scope

<?php
session_start();


$db_user="root";
$db_pass="";

global $con; // mark it as globally scoped

try {
  $con = new PDO('mysql:host=localhost;dbname=hrm', $db_user, $db_pass);
  $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}

<?php

require_once 'dbconfig.php';
include_once 'Class/class.user.php';
global $con; // mark it as globally scoped
$user= new user($con);


// VERIFICAR SE ESTÁ LOGADO
 if($user -> logado()==""){
   $user->redirect('login.php');

}


class user
{
   private $db;

   function __construct($con){
      // include_once '..\dbconfig.php'; // removed this, redundant
      $this->db=$con;
    }
}

E.g. 2: using return from a file

<!doctype html>


<?php

require_once 'dbconfig.php';
$con = include_once 'Class/class.user.php';

$user= new user($con);


// VERIFICAR SE ESTÁ LOGADO
 if($user -> logado()==""){
   $user->redirect('login.php');

}


<?php
session_start();


$db_user="root";
$db_pass="";


try {
  $con = new PDO('mysql:host=localhost;dbname=hrm', $db_user, $db_pass);
  $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}

return $con; // return the variable here


<?php

class user
{
   private $db;

   function __construct($con){
      // include_once '..\dbconfig.php'; // removed this, redundant
      $this->db=$con;
    }
}
geoidesic
  • 4,649
  • 3
  • 39
  • 59