You can call the mysql_change_user() function to change the user and database on a specified connection.
Syntax
my_bool
mysql_change_user(MYSQL *mysql,
const char *user,
const char *password,
const char *db)
It changes the user and causes the database specified by db to become the default (current) database on the connection specified by mysql. If you do not want to set a default database, you can specify db to NULL.
Return values
0 is returned for a setting success, and a non-zero value is returned if an error occurred.
Errors
Most errors that you get from calling this function are the same as those from mysql_real_connect(). This function may also return the following errors:
CR_COMMANDS_OUT_OF_SYNC: Commands were executed in an improper order.CR_SERVER_GONE_ERROR: The connection to the OBServer was disconnected.CR_SERVER_LOST: The connection to the server was lost during the query.CR_UNKNOWN_ERROR: An unknown error occurred.ER_UNKNOWN_COM_ERROR: The OBServer (probably an old server) does not implement this command.ER_ACCESS_DENIED_ERROR: The user or password was wrong.ER_BAD_DB_ERROR: The database did not exist.ER_DBACCESS_DENIED_ERROR: The user did not have the access privilege on the database.ER_WRONG_DB_NAME: The database name was too long.
Notes
If the logged-on user cannot be authenticated or does not have the privilege to use the database, mysql_change_user() fails, and the user and database are not changed.
mysql_change_user() will reset the session status. It always performs a ROLLBACK operation for active transactions, closes and drops all temporary tables, and unlocks all locked tables. It resets session system variables to the values of the corresponding global system variables, releases prepared statements, closes HANDLER variables, and releases locks obtained by using GET_LOCK().