Make WordPress Core

Opened 10 years ago

Closed 9 years ago

Last modified 8 months 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:


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 (6)

#1 follow-up: @foolswisdom
10 years ago

orvar, you are using which version of WordPress?

#2 in reply to: ↑ 1 @orvar
10 years ago

  • Version set to 2.1.2

Replying to foolswisdom:

orvar, you are using which version of WordPress?

sorry, 2.1.2

#3 @orvar
10 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?

#4 @rob1n
9 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.

This ticket was mentioned in Slack in #core by nvartolomei. View the logs.

14 months ago

This ticket was mentioned in Slack in #core by dd32. View the logs.

8 months ago

Note: See TracTickets for help on using tickets.