How do you use placeholders and SQL injection prevention with a short example?

In Perl, using placeholders in your SQL queries is crucial to prevent SQL injection attacks. Placeholders allow you to safely insert user input into your queries without concatenating user data directly into the SQL strings. Below is a short example that demonstrates how to use placeholders with the DBI module in Perl.

Keywords: Perl, SQL Injection Prevention, Placeholders, DBI Module
Description: This example illustrates how to use placeholders in Perl to securely handle user input and prevent SQL injection vulnerabilities in database operations.
#!/usr/bin/perl use strict; use warnings; use DBI; my $dbh = DBI->connect("DBI:mysql:database_name", "username", "password", { RaiseError => 1, PrintError => 0, }); my $username = 'example_user'; # User input my $sth = $dbh->prepare("SELECT * FROM users WHERE username = ?"); $sth->execute($username); while (my @row = $sth->fetchrow_array) { print "User ID: $row[0], Username: $row[1]\n"; } $sth->finish; $dbh->disconnect;

Keywords: Perl SQL Injection Prevention Placeholders DBI Module