What are potential pitfalls when storing values from a database in variables in PHP?

One potential pitfall when storing values from a database in variables in PHP is SQL injection attacks if the values are not properly sanitized. To prevent this, always use prepared statements with parameterized queries when interacting with the database. Additionally, be mindful of data type conversions and ensure that the variables are properly initialized before use.

// Example of using prepared statements to store values from a database in variables

// Assuming $conn is the database connection object

// Prepare a statement
$stmt = $conn->prepare("SELECT name, age FROM users WHERE id = ?");
$id = 1; // Example ID
$stmt->bind_param("i", $id);

// Execute the statement
$stmt->execute();

// Bind the results to variables
$stmt->bind_result($name, $age);

// Fetch the results
$stmt->fetch();

// Now $name and $age contain the values from the database