I am in the process of moving from hosted web sever to a VPS server I have setup with Apache, mysql, php etc. The following code works fine on my hosted server but throws an error on my VPS server
Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'groups' at line 1
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$host = 'localhost';
$db = 'dbname';
$user = 'user';
$pass = 'pass';
$charset = 'utf8';
$dsn = "mysql:host=$host; dbname=$db; charset=$charset";
echo $dsn .'<br>';
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
try {
$dbh = new PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) {
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
$sql = "SELECT * FROM groups";
$sth = $dbh->prepare($sql);
$sth->execute();
If I modify the SQL query to also include the database name e.g.
SELECT * FROM dbname.groups
Then it works fine, however I am at a loss to understand why I need to specify the database name in the query for it to work when the database is already selected in the DSN and it doesn't require it on my hosted server. I'm convinced it must be to do with some setting on my new VPS but cannot figure out what it may be and have a lot of code so really don't want to have to go through and alter every single SQL query to include the database name as well.
Hosted server is PHP 7.4.24 VPS server is PHP 7.4.3
Can anyone help?