I used to have one function to any select query in my script, but since it’s not secure I need to find out how to make a dynamic PDO class in php.
For example, I used to use this code.
function excute_query ($Query)
{
if($Query_Run = mysql_query($Query))
{
while($DATA = mysql_fetch_array($Query_Run, MYSQL_ASSOC))
{
$data_array[] = $DATA;
}
return $data_array;
close();
}
else{
die(mysql_error());
}
mysql_close();
}
and I can use it anywhere in my script and it return’s an array with the data. in 2 lines.
$sql = 'SELECT * FROM users';
$users = execute_query($sql);
Now I need to use the same function with PDO. I’ve been trying, but how I can make a class or function to do the same using PDO.
What I’m trying to say. instead of write same 4 lines to make a query, is there any way to make a function or class take a query and return the data ?
Thank you.
A quick and dirty but little verbose solution would be this one
but personally I hate writing the word
array, so, I would usefunc_get_args()and call this wayBut having a class would be apparently better.
It will let you have different functions for the different result sets, like
instead of your current
or, even more useful ones
to get an array where keys filled with user ids or