Name

mysql_change_user( )

Synopsis

my_bool mysql_change_user(MYSQL *mysql, const char *user, 
                          const char *password, 
                          const char *password, 
                          const char *database)

Use this to change the current user for the MySQL session to the one given as the second argument. The password of the new user is given in the third argument. This function will end the current session if successful, so it will need to issue a new USE statement. Therefore, a database to use is to be given as the fourth argument.

...
mysql = mysql_init(NULL);
mysql_connect(mysql,"localhost","hui","shorty");
mysql_select_db(mysql,"workrequests");
...
mysql_change_user(mysql,"russell","password","workrequests");
mysql_query(mysql, "UPDATE workreq
                     SET tech_person_id = '1015'
                     WHERE tech_person_id = '1012'");
...

In this example, the program begins with one user for running SQL statements, which are replaced with ellipses. However, for changing a sensitive data column (i.e., the person assigned to perform the work requests), the user is changed to one who has the proper authority.

Get MySQL in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.