What is the correct way to assign a database entry to a variable in PHP?

When assigning a database entry to a variable in PHP, you need to establish a connection to the database, query the database to retrieve the desired data, and then assign the result to a variable. This can be achieved using PHP's PDO or mysqli extension to interact with the database.

// Establish a connection to the database
$pdo = new PDO('mysql:host=localhost;dbname=database_name', 'username', 'password');

// Query the database to retrieve the desired data
$stmt = $pdo->query('SELECT column_name FROM table_name WHERE condition');

// Assign the result to a variable
$data = $stmt->fetchColumn();