Find answers, ask questions, and connect with our
community around the world.

Activity Forums Web Design Why can’t I seem to include a php file that has variables in it, in another php file?

  • Why can’t I seem to include a php file that has variables in it, in another php file?

    updated 3 weeks, 1 day ago 0 Member · 1 Post
  • Maverick

    Member
    October 28, 2019 at 6:47 pm

    I know this may seem like a very simple, stupid problem, but I’m actually really stuck on this. I have a user object that works perfectly fine, if I define the login information for my database inside of the class, but this isn’t what I want to do. I want the login information to be exclusively held in another php file dedicated to this purpose. This is the working code, there’s nothing wrong with it at all. class User { public $userID; public $userLogin; public $userFirstName; public $userLastName; public $userEmail; public $userPass; public $userPermissions; public $userVerified; public function __construct($username) { $servername = “localhost”; $dbusername = “root”; $dbpassword = “root”; $dbname = “camagru”; try { $conn = new PDO(“mysql:host=$servername;dbname=$dbname”, $dbusername, $dbpassword); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch(PDOException $e) { header(“Location: index.php?error=PDOerror”); } $sql = “SELECT * FROM users WHERE userLogin=?;”; $stmt = $conn->prepare($sql); $stmt->execute([$username]); $user = $stmt->fetch(); $this->userID = $user[“userID”]; $this->userLogin = $user[“userLogin”]; $this->userFirstName = $user[“userFirstName”]; $this->userLastName = $user[“userLastName”]; $this->userEmail = $user[“userEmail”]; $this->userPass = $user[“userPass”]; $this->userPermissions = $user[“userPermissions”]; $this->userVerified = $user[“userVerified”]; $conn = null; } public function dump() { echo “”; var_dump($this); echo “”; } } ?> Now, I want to remove servername, dbusername, etc from the code, because I have a file called “database.php” that looks like this. I just want to include this file and let my class refer to these variables that way. The problem is that when I enter this ABOVE and outside of my class require_once “../database.php”; everything stops working. The pages no longer load correctly. I have no errors to give you, unfortunately, but here is my entire file, when it’s in its broken state. Why would this not work? Is there something obvious I’m missing? Is there a different way to pass these variable around, because I’m going to have to pass these login details to a LOT of files in the future. – by hq overview monkey_programmer – –

Reply to: Maverick
Your information:

Cancel
Original Post
0 of 0 posts June 2018
Now