What are the potential pitfalls of using wildcards in MySQL queries for PHP applications?

Using wildcards in MySQL queries can leave your application vulnerable to SQL injection attacks if not properly sanitized. To mitigate this risk, always use prepared statements with parameterized queries when incorporating user input into your SQL queries.

// Example of using prepared statements with parameterized queries in PHP
$mysqli = new mysqli("localhost", "username", "password", "database");

if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}

$stmt = $mysqli->prepare("SELECT * FROM users WHERE username = ?");
$stmt->bind_param("s", $username);

$username = $_POST['username'];
$stmt->execute();

// Process the results
$result = $stmt->get_result();

while ($row = $result->fetch_assoc()) {
    // Do something with the data
}

$stmt->close();
$mysqli->close();