In the official documentation:
PDO::MYSQL_ATTR_INIT_COMMAND (integer)
Command to run when connecting to Mysql server. It will be executed again when reconnecting.
Note that this constant can only be used in the driver_options array
build a new database handler.
I mean, it’s a command mysql
which will run only once every connection, as soon as you connect (or reconnect) to the server.
Example of use:
$db = new PDO('mysql:dbname=mydb;host=localhost;port=3306', $user, $pass,
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'")
);
The command SET NAMES 'UTF8'
will run once, whenever this new connection starts.
Okay, Ricardo, thank you! But I’m still having doubts! I know he will run once when he connects, but, what does he do? It only serves to set the database to UTF8, etc...? Or I run what I want there?
– Lucas de Carvalho
@Lucascarvalho you can run any valid command.
– Ricardo Moraleida
Got it. Thank you Ricardo!!
– Lucas de Carvalho