Reputation: 1344
I'm having trouble on using a pdo based function
my GetCurrentLanguage()
function returns an int of 0 or 1
public function GetCurrentLangName()
{
$stmt = $GLOBALS['website']->prepare("SELECT * FROM available_languages WHERE id = :id");
$stmt->bindParam(':id', $this->GetCurrentLanguage(), PDO::PARAM_INT);
$stmt->execute();
$fetch = $stmt->fetchAll();
return $fetch['name'];
}
and is not working, it returns
Notice: Undefined index: name
Upvotes: 0
Views: 670
Reputation: 45490
fetchAll()
returns two dimensional array, so instead of returning $fetch['name'];
it should be
return $fetch[0]['name'];
or better use fetch
to return a single row :
$fetch = $stmt->fetch();
return $fetch;
then use function like this:
$data = GetCurrentLangName();
$language = $data['name'];
Upvotes: 0
Reputation: 805
you should set parameter as a param you want to bind.
public function GetCurrentLangName($id)
{
$stmt = $GLOBALS['website']- >prepare("SELECT * FROM available_languages WHERE id = :id");
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->execute();
$fetch = $stmt->fetchAll();
return $fetch['name'];
}
Upvotes: 0
Reputation: 41885
If you're expecting many rows, you could have just loop it.
public function GetCurrentLangName()
{
$data = array():
$stmt = $GLOBALS['website']->prepare("SELECT * FROM available_languages WHERE id = :id");
$stmt->bindParam(':id', $this->GetCurrentLanguage(), PDO::PARAM_INT);
$stmt->execute();
$fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($fetch as $row) {
$data[] = $row['name'];
}
return $data;
}
This function (->fetchAll()
) returns a multi-dimentional array. May look like this:
Array
(
[0] => Array
(
[id] => 1
[name] => lang
)
[1] => Array
(
[id] => 2
[name] => lang
)
)
Could have used print_r()/var_dump()
on $fetch
and you'll see what it yielded.
Upvotes: 2