WordPress.org

Make WordPress Core

Opened 7 years ago

Closed 7 years ago

#4090 closed defect (bug) (wontfix)

$wpdb->get_var("SELECT 1 FROM ... ") not working

Reported by: orvar Owned by:
Milestone: Priority: normal
Severity: normal Version: 2.1.2
Component: General Keywords: get_var wpdb
Focuses: Cc:

Description

Using queries like

$a = $wpdb->get_var("SELECT 1 FROM ... ")

to confirm the existence of something in a table does not work ($a is not set to 1). wpdb seems to have a problem with the fact that the column has a numerical name in the result as

$a = $wpdb->get_var("SELECT 1 AS x FROM ... ")


works fine ($a == 1).

Change History (4)

comment:1 follow-up: foolswisdom7 years ago

orvar, you are using which version of WordPress?

comment:2 in reply to: ↑ 1 orvar7 years ago

  • Version set to 2.1.2

Replying to foolswisdom:

orvar, you are using which version of WordPress?

sorry, 2.1.2

comment:3 orvar7 years ago

This problem stem from mysql_fetch_object which is being used in wpdb->query. Selecting any numerical value, even if its in quotes, results in an empty object (eg. "SELECT 1000,'123',2007 FROM ...").

Maybe the Codex should just reflect that get_var("SELECT 1 FROM ...") does not work (its a fairly common practice) -- while alternatives like get_var("SELECT 1=1 FROM ...") and get_var("SELECT TRUE FROM ...") for some reason do work (TRUE is an alias for 1 in mysql).

This could though be fixed with a query filter in the core -- but is it worth it?

comment:4 rob1n7 years ago

  • Milestone 2.3 deleted
  • Resolution set to wontfix
  • Status changed from new to closed

No, I don't think it is.

This is mainly because object vars can't start with numbers -- only A-Z or _. I don't see a very big need for this, and, as you said, there are other ways.

Note: See TracTickets for help on using tickets.