888888.888888.88""Yb..dP"Yb..8888b..Yb..dP.88b.88....db....8b....d8.88..dP""b8..dP"Y8
88__...88__...88__dP.dP...Yb..8I..Yb.YbdP..88Yb88...dPYb...88b..d88.88.dP...`".`Ybo."
88""...88""...88"Yb..Yb...dP..8I..dY..8P...88.Y88..dP__Yb..88YbdP88.88.Yb......o.`Y8b
88.....888888.88..Yb..YbodP..8888Y"..dP....88..Y8.dP""""Yb.88.YY.88.88..YboodP.8bodP'


88b.88.888888.888888.Yb........dP.dP"Yb..88""Yb.88..dP
88Yb88.88__.....88....Yb..db..dP.dP...Yb.88__dP.88odP.
88.Y88.88"".....88.....YbdPYbdP..Yb...dP.88"Yb..88"Yb.
88..Y8.888888...88......YP..YP....YbodP..88..Yb.88..Yb

May 23, 2008

I’m stuck on a SQL query for my next WordPress plugin. I have tried Google but I am coming up empty, perhaps becasue I don’t really know what to search for, so I am hoping that one you guys can help me out with some advice.

Here’s the problem:

I have a table and in it are two important columns (There are others but they don’t matter). The first is called ‘Names’ and the second is called ‘Values’

I want to run a query that will return a single row. Each column in that row will be have the name that is stored in the ‘Names’ column in the original table, and the value that is stored in the ‘Values’ column.

The diagram below should hopefully make it a little clearer.

Database Query

Does anyone have any ideas?

Adding Settings To Admin Pages
Dosettingsfields?
3 comments
page 793
How To Add Sidebars To A Theme
How to add one more sidebar to wordpress?
10 comments
page 1053
Wp Polls Reviewed
Image based poll?
one comment
page 58
3 Ways To Speed Up Your Blog Without A Cache Plugin
Comment on a few ways to speed up your blog?
one comment
page 1321
Updating Code Snippets Here
Code snippets?
one comment
page 1338
Wordpress Chat
Wordpress plugins chat?
3 comments
page 1308
Post Image The Easy Peasy Way
Wordpress retrieve post images individually?
26 comments
page 1065
Post Image The Easy Peasy Way
Calling for images using php wordpress?
26 comments
page 1065
Wpunlimited The Ultimate Wordpress Theme
Html5 wordpress?
3 comments
page 1141
Fun With Theme Widgets
Funy photo widget?
24 comments
page 867
Quick N Dirty Admin Login Screen
Login page css templates?
no comment
page 128
Wpunlimited The Ultimate Wordpress Theme
Html5 wordpress?
3 comments
page 1141
Post Image The Easy Peasy Way
Wordpress attach image to multiple posts?
26 comments
page 1065
Using Your Own Url Shortener
Wordpress clear rewrite rules?
4 comments
page 1190
Quick N Dirty Category Redirection
Wordpress redirect category?
no comment
page 133
Theming Habari Vs Wordpress
How to design a habari theme?
13 comments
page 440
Html 5 Gallery
Themeatic html5?
6 comments
page 1305
Premium Ithemes Review Photo Gallery
Ithemes review?
4 comments
page 226
Updating Code Snippets Here
Wordpress fun plugins?
one comment
page 1338
Updating Code Snippets Here
Wordpress fun?
one comment
page 1338
Why I Ditched Disqus
Disqus email?
5 comments
page 1175
Html 5 Gallery
Picture gallery html5?
6 comments
page 1305
Post Image The Easy Peasy Way
Wordpress first image?
26 comments
page 1065
Quick N Dirty Admin Login Screen
login screen using css?
no comment
page 128
Post Image The Easy Peasy Way
Resizing images in thecontent?
26 comments
page 1065
Post Image The Easy Peasy Way
Wordpress first image gallery?
26 comments
page 1065
Html 5 Gallery
Html 5 photo gallery?
6 comments
page 1305
Post Image The Easy Peasy Way
Inserting an image url in comments?
26 comments
page 1065
Post Image The Easy Peasy Way
Wp attachment functions?
26 comments
page 1065
Dont Mess With My Toot Toot
Wordpress custom content?
16 comments
page 599
Using Your Own Url Shortener
String shortner?
4 comments
page 1190
Quick N Dirty Admin Login Screen
My admin login page?
no comment
page 128
Using Your Own Url Shortener
How to build a shortner link?
4 comments
page 1190
  1 query every 899 seconds, updated 1 seconds ago.
Wednesday, 5pm
 __
(__)
   `

 Andrew

Thanks Jazz. I have resolved it now.

Wednesday, 2pm
 __
(__)
   `

 Jazz

Just browsing over and I saw this message..

Warning: Invalid argument supplied for foreach() in /home/fhlinux162/w/wp-fun.co.uk/user/htdocs/wp-content/plugins/fun_with_in_context_comments.php on line 340

Just to let you know.

Friday, 5pm
 __
(__)
   `

 Andrew Rickmann

Thanks Eric, I’ll take a look at that. The reason I want to do it in SQL instead of PHP is that I am doing a join with another query and I want to return a single result in a way that is consistent with the native WordPress query.

Friday, 4pm
 __
(__)
   `

 Eric

http://en.wikibooks.org/wiki/Programming:MySQL/Pivot_table

A pivot is what you were looking for, but my first way would work just as well and is a little more understandable. (IMO)

Friday, 3pm
 __
(__)
   `

 Eric

You are fundamentally changing your data model there. You could have 0..n columns returned, and i dont know how you would handle that in a static sql query without knowing the result first.

why not:
select names, values from table where id = 1

then

foreach(result) {
add it into an array
}

Your data model “Query Results” is really an array.

Hope that helps.