Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用函数中的PDO返回一些值_Php_Mysql_Arrays_Pdo - Fatal编程技术网

Php 使用函数中的PDO返回一些值

Php 使用函数中的PDO返回一些值,php,mysql,arrays,pdo,Php,Mysql,Arrays,Pdo,我使用PDO来获取表的一些值,如:(表名为ban) 我的职能是: 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;

我使用PDO来获取表的一些值,如:(表名为ban)

我的职能是:

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;
}
在主php文件中,我想用以下代码将它们取回:

$a = array();
$a = retBans();
$b = explode(",",$a);
print_r($b);
我想要这个:

Array {
[0] => one
[1] => two
[2] => three
[3] => four
}
但是,它只返回并打印数组中的最后一个值(四)

我怎样才能像我说的那样得到它们

改用这个-

$a = '';
while ($row = $result->fetch(PDO::FETCH_ASSOC)){
  $a .= $row['word'].",";
}
然后,您可以使用
explode
功能

$a = retBans();
$b = explode(",",$a);
echo "<pre>"; print_r($b);
$a=retBans();
$b=爆炸(“,”,$a);
回声“;印刷品(乙元);;

不需要explode如果您想要数组,只需使用
$a[]=$row['word']
@Ghost我尝试了它,但函数没有返回任何结果:
警告:explode()希望参数2是字符串
它不能与当前代码一起工作,因为您每次都在循环中覆盖$a,您应该使用
=
而不是
=
。您最好使用Ghost'appraoch,虽然您当前正在执行X以撤消X,但它没有sensetnx,但数组现在是空的
array([0]=>)
请使用您更改的代码更新您的问题,并尝试在分解之前打印\r($a),以检查是否所有数据都已修复,函数中返回$a:)
$a = retBans();
$b = explode(",",$a);
echo "<pre>"; print_r($b);