Jump to content

TheRebellion

New Members
  • Posts

    4
  • Joined

  • Last visited

TheRebellion's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. @mac_gyver - trying to create a class to use to connect to the database. I was able to get it to work.
  2. I have modified classes.Config.php to be the following: <?php class Config { private $_config; public function __construct() { $this->_config = include('variables.php'); } public function __get($key) { if (isset($this->_config[$key])) { return $this->_config[$key]; } return null; } } I am getting the same error but did see that user error in typing.
  3. Thank you. The error in PHP Storm that I am getting is: Undefined variable '$db_user' Undefined variable '$db_token' The error is being produced in test.php When I attempt to run it both db_user and db_token are blank. They are not returning any value. I have tried it a different way here are my files: classes/variables.php <?php return array ( 'db_user' => 'test', 'db_token' => 'test' ); classes/Config.php <?php class Config { private $_config; public function __construct() { $this->_config = include('variables.php'); } public function __get($key) { if (isset($this->config[$key])) { return $this->_config[$key]; } return null; } } classes/connection.php <?php class Connection { private string $server = "mysql:host=localhost;dbname=test"; private array $options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,); protected $con; public function openConnection($user,$token) { try { $this->con = new PDO($this->server, $user, $token ,$this->options); return $this->con; } catch (PDOException $e){ echo "There is some problem in connection: " . $e->getMessage(); } } public function closeConnection(): void{ $this->con = null; } } test.php <?php require_once "classes/Config.php"; require_once "classes/connection.php"; try { $config = new Config(); $user = $config->$db_user; $token = $config->$db_token; $database = new Connection(); $db = $database->openConnection($user,$token); echo "Connection Made"; } catch (PDOException $e) { echo "There is some problem in connection: " . $e->getMessage(); } echo "<br />Test";
  4. I have a file that is called databaseimports.php this file includes a server, username and password. The file does not get checked into git due to the .gitignore. I would like to directly import the variables in classes/variables.php to the class and assign them to $username and $token in the class. Is this possible? Am I missing something very simple? <?php include_once "classes/variables.php"; class Connection { private string $server = "mysql:host=localhost;dbname=test"; private array $options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,); protected $con; private string $username = $user; private string $token = $pass; public function openConnection($username, $token) { try { $this->con = new PDO($this->server, $this->username,$this->token,$this->options); return $this->con; } catch (PDOException $e){ echo "There is some problem in connection: " . $e->getMessage(); } } public function closeConnection(): void{ $this->con = null; } }
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.