简体   繁体   中英

How to access column-values before fetching?

I need the values of one columns before fetching the query. Something like this:

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

My query:

$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);
}

Well, how can I access this array [0]=>102, [1]=>64, [2]=>563, [3]=>79 before that while() in the above code?

Note: Preferably without the use of fetchAll();

Your question is like saying How can I fetch data without fetching it ?

This is obviously impossible, you will need to fetch it at some point in your application.


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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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