PDO Exception when trying to insert a row

Hello everybody

I’ve an access denied error to my user when I try to insert a row in a table, and I’m using PDO to connect to my database and I coudn’t figure out what’s wrong, could someone help me please ? I have a file config.php that contains the constants to the database connection values, as db_host, db_name, db_user and db_pass.
Here is my code…

(…)
try {
self::$instance = new PDO(‘mysql:host’.DB_HOST.’;dbname=’.DB_NAME,DB_USER,DB_PASS)
self::$instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); self::$instance->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
} catch (PDOException $e) {
echo $e->getMessage();
}
(…)

Follows the error message:

Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1044 Access denied for user ‘xxx_app’@’%’ to database ‘information_schema’ in /storage/ssd3/087/2516087/public_html/classes/Perfil.php:34 Stack trace: #0 /storage/ssd3/087/2516087/public_html/classes/Perfil.php(34): PDOStatement->execute() #1 /storage/ssd3/087/2516087/public_html/classes/teste_perfil.php(19): Perfil->insert() #2 {main} thrown in /storage/ssd3/087/2516087/public_html/classes/Perfil.php on line 34

Thanks in advance for help!

Mary

You have to define wich database you are connecting to :slight_smile:

1 Like

Hi Ckhawand,
Thanks for your help.
When I try to open a database connection, I’m using 4 values defined in config.php file: host name, database name, user and password.
Sorry, I couldn’t understand what is missing in the command.
Could you please send me an example?
Thanks again :slight_smile:

Oh, look at this…
I just adjusted the blank spaces before and after the equal (=) symbol and replaced single quotes.

self::$instance = new PDO(“mysql:host = “.DB_HOST.”;dbname=”.DB_NAME,DB_USER,DB_PASS);

Now it is working fine!!! :slight_smile:

1 Like

Nice to hear you have fixed it.

If you have any other problem, let us know. :wink:

1 Like

Thanks a lot!!!

Have a nice week :wink:

1 Like