What alternatives to mysql_escape_string can be used to escape special characters in PHP without affecting line breaks?
When using mysql_escape_string to escape special characters in PHP, line breaks are also affected, which can lead to formatting issues in text. To avoid this problem, one alternative method is to use the mysqli_real_escape_string function, which performs the same task of escaping special characters without affecting line breaks.
// Using mysqli_real_escape_string to escape special characters without affecting line breaks
$mysqli = new mysqli("localhost", "username", "password", "database");
// Assuming $input contains the user input that needs to be escaped
$escaped_input = $mysqli->real_escape_string($input);
// Now $escaped_input can be safely used in SQL queries without affecting line breaks
Related Questions
- Is binding a method of an object as a callback function in preg_replace_callback a recommended approach to avoid using global variables?
- How can PHP code be structured to handle different actions based on radio button selections in a form?
- What are the potential issues with PHP sessions being appended multiple times to URLs?