What I have
// Make an attempt to connect to the database
try {
// Make our connection
$DataBaseHandle = new PDO($Host,$Username,$Password,$Table);
$DataBaseHandle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES, false);
echo 'Connected!<br/>';
// This will give us a output as an average
$SQLQuery = "SELECT
CONCAT('$',TRUNCATE(AVG(`payper`),2)) AS `Average Pay`
FROM `rep_commission`
";
foreach ($DataBaseHandle->query($SQLQuery) as $row)
{
echo $row["Average Pay"];
}
$DataBaseHandle = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
die();
}
What I am trying to do
I am rewriting a bunch of my MYSQL_ files as we all know they're deprecated. I am also trying to prevent injection possibilities for grins. Scripts will be behind a employee dashboard, but want to take extra measures in case we have a smart employee with malicious intent.
Read here Are PDO prepared statements sufficient to prevent SQL injection?
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
does the trick???
I am also learning :D
I am curious where I am going wrong, as I am getting a
E_WARNING : type 2 --
PDO::__construct() expects parameter 4 to be array, string given
-- at line 11
And
E_ERROR : type 1 --
Call to a member function setAttribute() on a non-object
-- at line 12
Where did I go wrong?
Also, if someone could show me how do I do a var_dump(), with the code that I have above. Im having a hard time understanding how to do it properly. Tried reading up on it but was lost :(