2

I have been working on getting a PDO connection to establish within a class for the better half of a day. My problem is that as soon as I try to hit the new PDO(...) I get nothing, no errors nothing. Whatever I have after that will never run but my console will still be operational waiting for input. I have tried connecting in a few places including the "Driver" file where again, it doesn't do anything after I run the new PDO(...) instantiation. Here is part of my Database Class:

namespace App\Database;
class MySQL
{
    private $Host;
    private $DBName;
    private $DBTable;
    private $DBUser;
    private $DBPassword;
    private $DBPort;
    private $PDO;
    private $parameters;
    private $bConnected = false;
    public $querycount = 0;

    function __construct($DBName, $DBTable)
    {
        $this->Host = getenv("DATABASE_HOST");
        $this->DBPort =  getenv("DATABASE_PORT");
        $this->DBUser = getenv("DATABASE_USER");
        $this->DBPassword = getenv("DATABASE_PASSWORD");
        $this->DBName = $DBName;
        $this->DBTable = $DBTable;
        $this->Connect();
        $this->$parameters = array();
    }
    private function Connect()
    {
        try{
            echo "I am the good child and always print".PHP_EOL;
            $this->PDO = new PDO("mysql:host=localhost;port=33061;dbname=store", "root", "password");
            echo "Please Print Me <3".PHP_EOL;
            $this->bConnected = true;
        } catch (PDOException $e){
            echo $e->getMessage();
            die();
        }
    }

I have checked values for Host, DBPort etc and in this code below I have all their values hard coded so nothing should be wrong due to the formatting of insert the variables in their proper slots. I know PDO is turned on because I can use it in other projects but my catch isn't catching anything, I can get the first echo to run but the second one never appears. Any help would be awesome because at this point I am scratching my head at why I can't get this to connect.

Thanks!

If I am unclear I can try and provide clarity and I can also show the driver file if need be but basically, it is just calling the constructor just fine and the connect function is giving us problems.

Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
kennyL
  • 150
  • 1
  • 8
  • Your defining the properties to use from the environment variables but not using them :/, also are you sure its port `33061` – Lawrence Cherone May 03 '18 at 05:34
  • See https://stackoverflow.com/questions/32648371/my-pdo-statement-doesnt-work – Lawrence Cherone May 03 '18 at 05:34
  • 1
    I hard coded the connection string to ensure that the connection string was correct and yes I am certain that it is port 33061 as on the docker container I have exposed 33061 to my computer as to not conflict with a different instance that is running. – kennyL May 03 '18 at 05:37
  • `PDOException` should be `\PDOException` as your in a namespace, also `$this->$parameters = array();` is not right. enable error reporting and see what it says. – Lawrence Cherone May 03 '18 at 05:40
  • 1
    I have error reporting on and get nothing I corrected the namespacing issue and still do not get past the `new PDO(...)` and I still receive no errors. I have even tried wrapping it in a `var_dump(...)` and it returns nothing. – kennyL May 03 '18 at 05:46
  • The real problem might simply that you haven't turned PHP's error messages on. Everything is going to be harder if you can't see errors. – Evert May 05 '18 at 03:00

1 Answers1

0

I have been going over each line of my code for about the last hour or so if not more. I found some discrepancies in my variables. I used in a couple places $this->$parameters or other variations which didn't seem to cause an error for some reason but rendered the code inoperable. I'm not sure why it wasn't throwing an error about the variables but it would seem that is what caused my issue.

Thank you for your time and help!

kennyL
  • 150
  • 1
  • 8