What are the benefits and drawbacks of embedding complex SQL queries directly into PHP code versus using stored procedures or functions?

Embedding complex SQL queries directly into PHP code can make the code harder to maintain and debug. It can also lead to SQL injection vulnerabilities if input data is not properly sanitized. On the other hand, using stored procedures or functions can help improve code organization, reusability, and security.

// Example of embedding a complex SQL query directly into PHP code
$query = "SELECT * FROM users WHERE age > 18 AND gender = 'male'";
$result = mysqli_query($connection, $query);

// Example of using a stored procedure in PHP code
$query = "CALL get_users(18, 'male')";
$result = mysqli_query($connection, $query);