[英]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.