Pafo
Pafo

Reputation: 143

Return some values with PDO in function

i am using PDO to get some values of a table like : (table name is ban)

ID   word
1    one
2    two
3    three
4    four

MY function is :

function retBans() {
global $connect;
$result = $connect->prepare("SELECT * FROM ban");
$result->execute();
$a = array();
while ($row = $result->fetch(PDO::FETCH_ASSOC)){
  $a = $row['word'].",";
}
return $a;
}

and in the main php file, i wanted to get them back with this code :

$a = array();
$a = retBans();
$b = explode(",",$a);
print_r($b);

I wanted to have this :

Array {
[0] => one
[1] => two
[2] => three
[3] => four
}

But , it just return and print_r the last value (four) in array.

How can i get them like i said ?

Upvotes: 0

Views: 68

Answers (1)

TBI
TBI

Reputation: 2809

Use this instead -

$a = '';
while ($row = $result->fetch(PDO::FETCH_ASSOC)){
  $a .= $row['word'].",";
}

Then, you can use explode function

$a = retBans();
$b = explode(",",$a);
echo "<pre>"; print_r($b);

Upvotes: 1

Related Questions