2

What is the counterpart of Command.SqlParameters.AddWithValue() of .NET in PHP? I want to avoid SQL injection, I just wonder if PHP already has the same method aside from escaping strings thru mysql_real_escape().

Thanks in advance!

1 Answer 1

1

PDO's prepare() method and PDOStatement::bindParam() or PDOStatement::bindValue()

The difference between the two is bindParam() binds a reference to a variable. The variable value can change without having to re-bind the parameter. This is particularly useful in loops.

bindValue() simply binds a static value to a parameter.

Sign up to request clarification or add additional context in comments.

2 Comments

Wow! It's more than what I need, that PDO also has transactions, commit and rollback. Thanks! By the way, I don't agree with execution of query in each iteration of loop. He he.
@domanokz It's just handy to know. Some DBs will only allow a single set of values in an insert statement. If you have an array or collection of entries to insert, it's nice and clean to bind once to the variables and execute the inserts while looping

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.