I would advise you to train yourself to use prepared statements. Especially since you are new to working with databases. The sooner you start using them, the easier it will become second nature.
For example, I did not know about prepared statements when I started with databases. And I experienced my own isolation when I contacted them. Because I'm already used to another way of doing something already. Now it may not be a trade in a character, but it does not hurt to start as soon as possible with him in any case.
Prepared statements allow you to use placeholders in your queries. Then these placeholders can be replaced with actual values, linking them to placeholders. This binding process automatically escapes values.
Here's a (simple) PDO example:
$db = new PDO( ); $statement = $db->prepare( 'INSERT INTO table VALUES( :username, :password )' ); $statement->bindValue( ':username', $dirtyUsername ); $statement->bindValue( ':password', $dirtyPassword ); $result = $statement->execute();
With PDOs and trained operators, there are more options. For example, you can easily reuse a prepared statement in a loop, as such:
$statement = $db->prepare( 'INSERT INTO table VALUES( :username, :password )' ); foreach( $users as $dirtyUser ) { $statement->bindValue( ':username', $dirtyUser->username ); $statement->bindValue( ':password', $dirtyUser->password ); $result = $statement->execute();
Or pass placeholder bindings to the execution method, for example:
$statement = $db->prepare( 'INSERT INTO table VALUES( :username, :password )' ); $result = $statement->execute( array( ':username' => $dirtyUsername, ':password' => $dirtyPassword ) );
... etc. etc.
Decent dabbler
source share