What are potential issues with using the exec() function in PHP to execute MySQL commands?
Using the exec() function in PHP to execute MySQL commands can pose security risks such as SQL injection attacks if user input is not properly sanitized. To prevent this, it is recommended to use prepared statements with parameterized queries to securely interact with the database.
// Establish a database connection
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');
// Prepare a SQL statement with a parameter
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = :username");
// Bind the parameter value
$stmt->bindParam(':username', $username);
// Execute the query
$stmt->execute();
// Fetch the results
$results = $stmt->fetchAll();
Related questions
-
PHP time differences convert
-
timestamps strings date comparisons
-
input validation data sanitization htmlspecialchars