Thanks for your answers.
The best result I can get at this moment is when I put the query in de connection.php. I can't get this working in the functions, always get an error on ->prepare
Now my connection.php looks like this:
<?php
class dbConnect {
public function connect($id, $column){
try{
$pdo = new PDO ("mysql:host=localhost;dbname=database", 'root', '');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = $pdo->prepare('SELECT * FROM users WHERE id = :id');
$query->bindParam(':id', $id);
$query->execute();
while($row = $query->fetch()) {
echo ($row[$column]);}
}
catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
}
}
?>
and functions.php looks like this:
<?php
require_once ('connection.php');
function getUserName($id, $column){
$pdo = new dbConnect();
$pdo->connect($id, $column);
}
$id = '1';
$column = 'username';
getUserName($id, $column);
function getFullName($id, $column){
$pdo = new dbConnect();
$pdo->connect($id, $column);
}
$id = '1';
$column = 'fullname';
getUserName($id, $column);
?>
It makes me able to create functions for returning every seperate column.
Is this the best way to do it?
I think it also would be better if I just query on the column, so user "username" in stead of *. But when I replace the * for :column and bindParam :column it only shows me "username" of "fullname" in stead of my name.