繁体   English   中英

在提取之前如何访问列值?

[英]How to access column-values before fetching?

在获取查询之前,我需要一列的值。 像这样:

// mytable
+----+-------+
| id | codes |
+----+-------+
| 1  | 102   |
| 2  | 64    |
| 3  | 563   |
| 4  | 79    |
+----+-------+

我的查询:

$db = new PDO("mysql:host=localhost;dbname=mydb", "root", "");
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sth = $db->prepare("SELECT * FROM mytable");
$sth->execute();

/* I need to those numbers in the codes-column as an array in here */

while ($results = $sth->fetch()) {
    var_dump($results);
}

那么,如何在上述代码中的while()之前访问此数组[0]=>102, [1]=>64, [2]=>563, [3]=>79

注意:最好不使用fetchAll();

您的问题就像是说如何在不获取数据的情况下获取数据?

这显然是不可能的,您将需要在应用程序中的某个时刻获取它。


if($results = $sth->fetch()) {
    var_dump($results);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM