Forums

RS-Linkify Thread is sticky

Quick find code: 261-262-33-65181208

Meredith wtf

Meredith wtf

Posts: 4,160 Adamant Posts by user Forum Profile RuneMetrics Profile
You don't need to escape database input if you parameterize queries (I use PDO)

They're especially nice because you don't need to send the query each time you want to make a request. You just send different parameters.

For example:

$query = "SELECT * FROM table_name WHERE user = :user";
$query_params = array(":user"=>trim($_GET['user']));

try{
$stmt = $this->db->prepare($query);
$result = $stmt->execute($query_params);
}catch(PDOException $ex){
die("Something went wrong!&quot ;)
;
}

print_r($stmt->fetchAll());

You should still do more to your $_GET than trim it, but you'll be pretty safe from SQL injection. For rs display names, just chop it at 12 characters, remove non-alphanumeric characters besides hyphens/underscores/spaces, and call it a day. RSN's are probably the safest things to take in as input :p

25-Jan-2014 04:24:48 - Last edited on 25-Jan-2014 04:25:25 by Meredith wtf

Meredith wtf

Meredith wtf

Posts: 4,160 Adamant Posts by user Forum Profile RuneMetrics Profile
Oh, in your constructor (or whereever you keep your db connection code), make sure you have

$this->db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

Where $this->db is your PDO class

Basically, PHP does a find and replace (ie swaps :user for Meredith, or :user with user; DROP TABLE table_name) by default, which defeats the entire purpose of parameters. With ATTR_EMULATE_PREPARES set to false, the database will handle the parameters

25-Jan-2014 04:28:22 - Last edited on 25-Jan-2014 04:30:36 by Meredith wtf

Cyanid
Jan Member 2006

Cyanid

Posts: 24,133 Opal Posts by user Forum Profile RuneMetrics Profile
meredith ur like my #3 fav hlfer.


..::;;::..
¸¤¹´`¹¤¸
Your Friendly, Neighborhood
¸¤¹´`¹¤¸
..::;;::..
`¹¤¸¸¤¹´
..::;;::..
`¹¤¸¸¤¹´
Mustache
`¹¤¸¸¤¹´
..::;;::..
`¹¤¸¸¤¹´

25-Jan-2014 04:31:06

Quick find code: 261-262-33-65181208 Back to Top