Can someone please tell me why my function is not working?
function myappsbdo($sqlquery, $tabname)
{
try
{
$pdo = new PDO("mysql:host=127.0.0.1;port=3306;dbname=myapps","root","");
}
catch (PDOException $e)
{
echo "Problème de connexion";
exit();
}
$sql = $sqlquery;
$result = $pdo->query($sql);
$tabname = $result->fetchALL(PDO::FETCH_NUM);
}
I do a var_dump of the variable I chose for my $tabname and it’s an empty array. There is suppose to have my db data in it…
Thanks!
EDIT: this is how I call it.
myappsbdo(“SELECT * FROM categorie”, $tab1);
The function argument
$tabnamewas passed by value, therefore your subsequent assignment to that variable changes only the value of the function-scoped variable$tabnameand not of the calling-scoped variable$tab1.You want to pass by reference instead:
Or, alternatively, return the resultset:
Note that you probably ought to make your PDO object static, so that the database connection can be reused in successive function calls.