What are the potential risks of allowing global database variables for PHP addons?
Allowing global database variables for PHP addons can pose security risks such as SQL injection attacks and data leaks. To mitigate these risks, it is recommended to use prepared statements and parameterized queries to sanitize user input and prevent malicious code execution.
// Example of using prepared statements to prevent SQL injection
// Connect to the database
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');
// Prepare a statement
$stmt = $pdo->prepare('SELECT * FROM users WHERE username = :username');
// Bind parameters
$stmt->bindParam(':username', $_POST['username']);
// Execute the statement
$stmt->execute();
// Fetch the results
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);