Query Current MySQL Username

  • Share this:

Code introduction


This function queries the current logged-in username from the MySQL database.


Technology Stack : MySQL database, mysql-connector-python

Code Type : Database query

Code Difficulty : Intermediate


                
                    
def fetch_random_user_name(connection):
    cursor = connection.cursor(dictionary=True)
    cursor.execute("SELECT user() AS username")
    result = cursor.fetchone()
    cursor.close()
    return result['username']