PDO associative arrays - return associative

前端 未结 4 846
我寻月下人不归
我寻月下人不归 2021-01-04 08:37

I have this code:

$dbInstance = DB_Instance::getDBO();
$statement = $dbInstance->prepare(\"SELECT id, name FROM la         


        
相关标签:
4条回答
  • 2021-01-04 09:00

    I think you might be looking for $result = $sth->fetch(PDO::FETCH_ASSOC);

    Reference: http://php.net/manual/en/pdostatement.fetch.php

    [edit] oops to late :)

    0 讨论(0)
  • 2021-01-04 09:05

    Everybody forgot about the

    $sth->fetchAll(PDO::FETCH_KEY_PAIR);
    
    0 讨论(0)
  • 2021-01-04 09:22

    Not really sure if there's any better way. You could try this?

    $rows = $statement->fetchAll(PDO::FETCH_ASSOC);
    
    $languages = array();
    
    function getLangs($col, $row) {
         $languages[$col['id']] = $col['name'];
    }
    
    array_walk($rows, 'getLangs');
    

    There's nothing wrong with foreach loops. I'd actually use what you've got. It's hard to get cleaner than that...

    UPDATE:

    After carefully re-reading your question, what you REALLY should be asking is whether you can format your QUERY in such a way that the results are returned in a different format.

    The way that your normal SELECT query is returned is thusly:

    +----+----------+
    | id |     name |
    +----+----------+
    |  1 |  svenska |
    |  2 | engelska |
    | .. |      ... |
    | .. |      ... |
    +----+----------+
    
    $row = array(
        row_1 => array(
            id   => "1",
            name => "svenska"
        ),
        row_2 => array(
            id   => "2",
            name => "engelska"
        ),
        row_3 => array(
            id   => "...",
            name => "..."
        ),
        row_4 => array(
            id   => "...",
            name => "..."
        )
    )
    
    $row[$row_number][$column_name] = $value
    

    What you're asking for is for some way to return your query results like THIS:

    // Query result is only one row, with each 'id' as column name
    // And the 'name' from the same row as it's value...
    
    +---------+----------+-----+-----+-----+
    |       1 |        2 | ... | ... | ... |
    +---------+----------+-----+-----+-----+
    | svenska | engelska | ... | ... | ... |
    +---------+----------+-----+-----+-----+
    
    $row = array(
        row_1 => array(
              1 => "svenska",
              2 => "engelska",
            ... => "...",
            ... => "...",
            ... => "..."
        )
    )
    
    $languages = $row[row_1];
    $languages[$id] = $name;
    

    I'm not entirely sure you CAN do this in SQL, to be perfectly honest. I would also recommend against it, even if you could. It would be horrendous for a scaling table. If your table is static, then why not format it in the way I just mentioned to begin with? Why not just have it in a static PHP array in an include file?

    0 讨论(0)
  • 2021-01-04 09:22
    <?php
    $dbhost = 'somehost';
    $dbuser = 'someuser';
    $dbpw = 'somepw';
    $dbname = 'somename';
    $options = [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,];
    $dsn = "mysql:host=$dbhost;dbname=$dbname;";
    $pdo = new PDO($dsn, $dbuser, $dbpw, $options);
    
    $data = $pdo->query("SELECT * FROM language")->fetchAll(\PDO::FETCH_UNIQUE);
    var_dump($data);
    

    FETCH_UNIQUE does the trick...

    0 讨论(0)
提交回复
热议问题