Okay, so you've already found out that you shouldn't be using the mysql_xxx()
functions. That's good.
The alternatives are the mysqli
and PDO
libraries.
Both of these libraries do offer equivalent functions to mysql_real_escape_string
, but they also offer a better alternative, called Parameterised Queries (or Prepared Statements).
This is an alternative method of writing queries with variables which is considered much better practice than using the escape string method.
What happens is that you define your query with placeholders rather than variables; something like this:
SELECT * FROM table WHERE id = :id
where :id
is the placeholder.
You call a function with the query as above with the placeholders, and then separate calls to associate your variables with each of the placeholders.
The exact code for this differs between the mysqli
and PDO
libraries, but in both cases the variables are sent to the DB server separately from the query, and thus there is no chance of a SQL injection attack.
The PDO library is generally considered the better option. Here are some links to good sites with examples of how to write the code:
Hope that helps.