I use prepared PDO statements, working on my current project, I decided to create several functions for each action that I need. Below is an example of extraction
function db_fetchAll($sql, $param) { global $db; $stmt = $db->prepare($sql); if (empty($param)) { $stmt->execute(); } else { $stmt->execute($param); } $count = $stmt->rowCount(); if ($count == 0) { $result = ""; } elseif ($count == 1) { $result[] = $stmt->fetch(); } elseif ($count > 1) { $result = $stmt->fetchAll(); } return $result; }
Example
$database = db_fetchAll("SELECT * FROM database_table WHERE id=:id", array(':id' => $id));
It only condenses query lines from 3 lines to 1, but with the amount of information needed for each page, it needs to be minimized.
I return to the project in one last pass, and I just wanted a second opinion on the security of this. If there is something that I should add, etc. All user_input are passed through this function.
function user_input($input) { $input = trim($input); $output = strip_tags($input); return $output; }
and all output uses htmlspecialchars.
So, the question in a nutshell: Is it safe? Is there anything else I could do to prevent other forms of injections, etc.
I fully understand how prepared statements work, I’m just more thorough, version 1 of this site was a nightmare, tons of injections, access to administrator accounts, etc.
source share