I am writing a CLI application in PHP and I can't seem to understand why my MySQL Class that uses PDO will not get past the PDO or even error. I have made sure error reporting was enabled in my php.ini. PHP version is 7.1.3 on Windows 10.

Here is the MySQL Class. Please note it is not the whole class.

<?php

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 "CENSOREDv1.0".PHP_EOL;
            $this->PDO = new PDO("mysql:host=127.0.0.1;port=33061;dbname=store", "root", "password");
            $this->PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $this->PDO->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);        
            echo "CENSOREDv1.1".PHP_EOL;
            $this->bConnected = true;
        } catch (\PDOException $e){
            echo $e->getMessage();
            die();
        }
    }

Please understand I have hard coded my PDO connection string as to attempt to remove that as a possible issue.

I have invoked my MySQL class in another file. The constructor and everything fires right up until the PDO connection string then nothing.

Please let me know if you would like any additional information and I will do my best to provide it.

Thank you in advance for any help!

Recommended Answers

All 12 Replies

If I remember right it is needed to access global class outside of the namespace.

Someone on stackoverflow pointed out it was “wrong” when it didn’t have it then proceeded to not help any further.

@kennyL. To see if there are any other clues, let's see your other posts on this. The URLs to your other discussions would be nice to see what others are thinking.

OK, what about line 32. "$this->PDO". Why that instead or just $this?
Example? See my first link.

$this->PDO is setting private $PDO;

Your local MySQL configuration has port 33061 open? No firewall blocking the connection?

I also doubted the need for the \PDOException but I havent used PDO in quite a while now... that said, the only way you are likely gonna solve this at this point is to echo/vardump after each step and see what you got.

It's possible that if the port or URL is not acceptable/accessible, PHP just hangs and waits and times out.

So it doesn't time out. I can connect with SQL Workbench to the Docker container running on my local machine. I have tested with the MySQL instance not running and it almost imediatly times out. Vardumping the $this->PDO provides no data at all which I also found really weird. I have also changed line 32 to new \PDO(...) as it not part of the namespace either.

I have had the script running for the past 5 minutes and recieved no error or timeout on the SQL request it just hangs still.

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!

At this point I worry that you are the point of nothing works in php+pdo. That is, I can't guess why the private and such were necessary at this point so my question is there more to this story. Like this is the first time this machine has been setup and tested with this system?

Also, what happens if you used programming examples with as few changes as possible?

PS. OP seems to have found it. They revealed some issue with variabled in their other thread.

I put it here first too :D just before your reply.

Thank you for your time and help!

commented: Thanks for that. It was bugging me why this was happening. +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.