//Connect to the database $conn = mysqli_connect("localhost","my_user","my_password","my_db"); //Retrieve user data from the 'users' table $sql = "SELECT * FROM users WHERE id = '123'"; $result = mysqli_query($conn, $sql); //Parse the result and display the user's name $user = mysqli_fetch_assoc($result); echo $user['name'];
//Start the session session_start(); //Store user data in the session $_SESSION['user_id'] = 123; $_SESSION['user_name'] = 'John Doe'; //Retrieve user data from the session $user_id = $_SESSION['user_id']; $user_name = $_SESSION['user_name'];This example stores user data in a session variable and retrieves it later. This can be useful for maintaining user state across multiple pages. Package/Library: None - session handling is built into PHP.