简体   繁体   中英

How can I get all rows of 1 column using PDO?

I'm new to PDO, I'm running a query like:

  <?php
    $query = "

    SELECT     c1, c2, c3, c4
    FROM       t1

    ";
    $conn = new PDO('oci:dbname=//127.0.0.1/XE', 'user', 'pass', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING));
    $stmt = $conn->prepare(trim($query));
    $stmt->execute();
    $data = $stmt->fetchAll();
    $stmt = null;
    ?>

Then I am using it like:

labels: ["<?php echo join($data[0], '","'); ?>"],

but this outputs the values of 4 columns from 1 row, but I want the values of all rows in 1 column. How can I do this?

For example here is the table:

c1          c2          c3          c4
--------------------------------------
1          500         200         300
2          200         700         400
3          600         800         100
4          100         300         200
5          800         600         200

I want the output to be

"1", "2", "3", "4", "5"

not

"1", "500", "200", "300"

For PHP >= 5.5

$c1 = array_column($data, 0);

for earlier versions of PHP

$c1 = array_map(
    $data, 
    function($value) {
        return $value[0];
    }
);

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