0

I am storing db credentials in a file as constant and including in another file. I am using PDO to connecting with database. But It's unable to differentiating between string and constant variables. So how to differentiate it to connect with database? This is my code line-

$db = new PDO("mysql:hostname='DB_HOST'; dbname='P_DB'", "'DB_USER'", "'DB_PASSKEY'");

1 Answer 1

1

PHP constants cannot be instantiated within a string; you need to concatenate them to the string, or, in the case of your user and password fields, just reference the constant:

$db = new PDO("mysql:hostname=" . DB_HOST . ";dbname=" . P_DB, DB_USER, DB_PASSKEY);
Sign up to request clarification or add additional context in comments.

2 Comments

You are right. Thank You. But there should be something which help to differentiate.
@MdFarzan this is why PHP8 will now flag an error if you use something like DB_USER without quotes but without defining it as a constant first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.