What are some best practices for storing and retrieving variables in MySQL in PHP?

When storing and retrieving variables in MySQL in PHP, it is best practice to use prepared statements to prevent SQL injection attacks and ensure data integrity. This involves using placeholders in the SQL query and binding the variables to the placeholders before execution. Additionally, it is recommended to sanitize user input to prevent any malicious code from being executed.

// Connect to MySQL database
$pdo = new PDO("mysql:host=localhost;dbname=mydatabase", "username", "password");

// Prepare SQL statement with placeholders
$stmt = $pdo->prepare("INSERT INTO mytable (column1, column2) VALUES (:value1, :value2)");

// Bind variables to the placeholders
$stmt->bindParam(':value1', $variable1);
$stmt->bindParam(':value2', $variable2);

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