Skip to content

SQLite database abstraction layer issue with SELECT id FROM {$wpdb->posts} #1

@yalogica

Description

@yalogica

I think, there is an issue with SQLite database abstraction layer. I encountered this issue in my GutenDraw plugin recently.
If we call this SQL below 'P.id' transforms to 'ID' in array output. The problem only affects the ID column.

global $wpdb;

$sql = "SELECT P.id, P.post_title FROM {$wpdb->posts} AS P";
$results = $wpdb->get_results($sql, 'ARRAY_A');

foreach ($results as $item) {
   foreach ($item as $key => $value) {
      echo $key . ': ' . $value . '   ';
   }
   echo '<br>';
}

output

ID: 1 post_title: Hello world!
ID: 2 post_title: Sample Page
ID: 3 post_title: Privacy Policy
ID: 4 post_title: Auto Draft
ID: 5 post_title: TEST

as result, we can't use such code, because it's empty

echo $item['id'];

To solve this problem, I had to change all the selects to this option, and hard-code the names for the columns, where 'id' is in lower case.

$sql = "SELECT P.id as id, P.post_title FROM {$wpdb->posts} AS P";

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions