#4090 closed defect (bug) (wontfix)
$wpdb->get_var("SELECT 1 FROM ... ") not working
Reported by: |
|
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)
#3
@
16 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
@
16 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.
8 years ago
This ticket was mentioned in Slack in #core by dd32. View the logs.
7 years ago
#7
follow-up:
↓ 8
@
7 years 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
@
7 years 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 :)
orvar, you are using which version of WordPress?