I have the following code to define a constant with the value of a variable:
define('DB_NAME', $row['co_database_name']);
and use it in a connection string
$dsn = "mysql:host=" . DB_HOST . ";dbname=" . DB_NAME . ";charset=" . DB_CHARSET . ";port=" . DB_PORT;
$opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_PERSISTENT => true,
];
$conn = new PDO($dsn, DB_USER, DB_PASS, $opt);
but gives error Undefined variable: conn
If I do it directly with the name of the database ... like this:
define('DB_NAME', 'nombre_base_datos');
It works well, why? Is there another way to do it?
Thank you.