WordPress.org

Make WordPress Core

Opened 10 years ago

Closed 10 years ago

Last modified 3 weeks 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 (8)

#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
10 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.


15 months ago

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


10 months ago

#7 follow-up: @puggan
3 weeks ago

If this is a "wontfix", could you at least update the manual (https://codex.wordpress.org/Class_Reference/wpdb), so it says that columnname cant be numeric.

#8 in reply to: ↑ 7 @SergeyBiryukov
3 weeks ago

Replying to puggan:

If this is a "wontfix", could you at least update the manual (https://codex.wordpress.org/Class_Reference/wpdb), so it says that columnname cant be numeric.

Codex is a wiki, anyone with a WordPress.org account can edit the documentation there, including yourself :)

Note: See TracTickets for help on using tickets.