Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.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,Mytable: +-----+-----------+ | id | word | |-----|-----------| | 1 | test1 | | 2 | test2 | | 3 | test3 | +-----+-----------+ while($end = mysql_fetch_assoc($result)){ echo $end["word"].' - '; } $result = $sth->fetchAll()

Mytable:

+-----+-----------+
| id  |   word    |
|-----|-----------|
|  1  |   test1   |
|  2  |   test2   |
|  3  |   test3   |
+-----+-----------+
while($end = mysql_fetch_assoc($result)){
 echo $end["word"].' - ';
}
$result = $sth->fetchAll();
print_r($result);
Mysql:

+-----+-----------+
| id  |   word    |
|-----|-----------|
|  1  |   test1   |
|  2  |   test2   |
|  3  |   test3   |
+-----+-----------+
while($end = mysql_fetch_assoc($result)){
 echo $end["word"].' - ';
}
$result = $sth->fetchAll();
print_r($result);
输出:

test1 - test2 - test3
PDO:

+-----+-----------+
| id  |   word    |
|-----|-----------|
|  1  |   test1   |
|  2  |   test2   |
|  3  |   test3   |
+-----+-----------+
while($end = mysql_fetch_assoc($result)){
 echo $end["word"].' - ';
}
$result = $sth->fetchAll();
print_r($result);
输出:它是两个嵌套数组,如果我想选择一条记录,我应该这样做:

echo $result[0][word]; // output=test1
echo $result[1][word]; // output=test2
echo $result[2][word]; // output=test3
while($end = $sth->fetch()) {
    echo $end["word"].' - ';
}
现在我想知道,有没有什么技巧可以让我在PDO中回显一列的所有记录


编辑:

+-----+-----------+
| id  |   word    |
|-----|-----------|
|  1  |   test1   |
|  2  |   test2   |
|  3  |   test3   |
+-----+-----------+
while($end = mysql_fetch_assoc($result)){
 echo $end["word"].' - ';
}
$result = $sth->fetchAll();
print_r($result);
如何以非手动方式回显数据。有两种手动方式:

echo $result[0][word]; // output=test1
echo $result[1][word]; // output=test2
echo $result[2][word]; // output=test3
那么我应该为1000行做什么呢?我当然不应该这样做:

echo $result[0][word]; // output=test1
echo $result[1][word]; // output=test2
.
.
.
echo $result[999][word]; // output=test1000

任何报价?

键表示行(从零开始),子数组保留列名为键的列值。

fetch()
返回一行数据,这将是一个字段数组


fetchAll()
返回ALL行数据,因此可以得到一个字段数组。

PDO与mysql_fetch_assoc的等价物是fetch:

fetchAll的作用类似于:

$result = array();
while($end = mysql_fetch_assoc($result)){
 $result[] = $end;
}
print_r($result);
因此,PDO中的代码如下所示:

echo $result[0][word]; // output=test1
echo $result[1][word]; // output=test2
echo $result[2][word]; // output=test3
while($end = $sth->fetch()) {
    echo $end["word"].' - ';
}

我看到了mysql\u fetch\u assoc()然后我看到了
fetchAll()
。不能同时使用mysql和PDO。从连接到查询必须保持相同的MySQL。或者,这是两者之间的一个为什么/比较问题的区别吗?@Fred ii-我在两个不同的例子中使用了
mysql\u fetch\u assoc()
fetchAll()
。那么你的问题就不清楚了。如果您能为将来的读者编辑您的问题,最好提及“使用两种不同的API有什么区别”之类的事实;-)就我而言,我以为你在混合MySQL API。有些人可能也不理解这个问题。请阅读手册,它都在里面。@Fred ii-我已经读过了,我不希望结果出现在数组中。如果pdo,我如何访问记录的数据?事实上,POD中的等价物是什么:while($end=mysql_fetch_assoc($result)){echo$end[“word”];}?如果我使用
fetch()
它只返回一个结果。POD中与此等效的是什么:
while($end=mysql\u fetch\u assoc($result)){echo$end[“word”]}
我应该何时使用
fetch()
?它是否等于
限制1
(在sql中)?检查否。您使用的是相同的结果集。在循环中使用fetch(),尤其是在构建结果结构时必须处理某些数据的情况下。e、 g.
while($row=fetch()){do stuff with$row}
,而调用fetchAll()只会立即向您抛出整个结果集。但是您的代码只会回显
test1
,因为您使用的是
fetch()
。我想要
test1-test2-test3
@stack:no,这两个示例都将返回所有行-
while()
循环执行此操作。这两个回迁(对于mysql库和PDO/mysql库)都将读取该行并将内部指针移动到下一条记录。