Ok I'm hoping someone can help me past this and I think it's gotta be something simple. I'm building a website using Apache, PHP, and MySQL. The code runs great on localhost, but not when I make a network connection (by IP).
Specs
- Windows 10
- Apache 2.4.58
- MySQL 8
- PHP 8.3
Problem
Quite simply, I'm executing a SQL statement and it returns a result using fetchAll. It works perfectly on localhost and returns the expected results. Accessing the same site/page by IP address from another machine (Windows 11) and the query returns an empty result (which causes a subsequent error due to null values).
Code
The code where the database connection is established is in an object that extends PDO:
parent::__construct($this->getDSN(),$this->getConnector("user"),$this->getConnector("password"));
parent::setAttribute(parent::ATTR_ERRMODE,parent::ERRMODE_EXCEPTION);
The code where the fetchAll happens:
public function fetchAll($style=\PDO::FETCH_ASSOC)
{
try {
$result = ($this->_queryType === self::SQL_SELECT ? $this->_statement->fetchAll($style) : null);
} catch(\Exception $e) {
print "DEBUG::".$e->getMessage();
}
return (!empty($result) ? $result : null);
}
Steps Taken:
Apache log files show no apparent PDO relate errors. The access.log shows:
127.0.0.1 - - [02/Feb/2024:13:44:06 -0500] "GET /beliefs.php HTTP/1.1" 200 25
nnn.nnn.nn.nn - - [02/Feb/2024:13:44:12 -0500] "GET /beliefs.php HTTP/1.1" 200 25
The DB connection appears to be intact and I traced the issue to a single line of code but as yet have no clue why it's behaving this way:
$result = ($this->_queryType === self::SQL_SELECT ? $this->_statement->fetchAll($style) : null);
Any help you can offer would be much appreciated.
Update
I dumped out the contents of all the variables/objects leading up to the execution of the fetchAll, including $this, just to make sure they all contained the values I thought they did. All them matched what I expected, with the exception of the $result array (which remains empty for non-localhost connections).
Also tested with my cell phone (Android) just to see if it was isolated to the Windows 11 laptop. No dice, same outcome as on the Windows 11.
Ok, found the issue.
Bottom line, the URL is different between localhost and client because I was accessing via IP address. Consider that:
The design of my solution checks a database for a given page's content based on the URL of the page (left side of the equation above). Hence when I access it by IP, it finds no corresponding record in the DB. The solution works as designed, but testing it via IP address is a limitation in this situation.