I have a table which stores the data related to posts in this format
describe wp_postmeta;
+------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------------------+------+-----+---------+----------------+
| meta_id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| post_id | bigint(20) unsigned | NO | MUL | 0 | |
| meta_key | varchar(255) | YES | MUL | NULL | |
| meta_value | longtext | YES | | NULL | |
+------------+---------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
The meta_key has a key like 'author', 'excerpt', 'download'. Meta value has corresponding values.
Now I am writing a php script that queries the DB based on a post_id. SO I need to display all the meta_data from the above table in an order specified. The query is:
$select_post_meta="select meta_value from wp_postmeta where post_id='".$postid."' and (meta_key='author' or meta_key='category' or meta_key = 'excerpt')";
$meta_paper = mysql_query($select_post_meta);
while(($meta_values = mysql_fetch_array($meta_paper))
{ echo "<i>'".$meta_values['meta_value']."'</i><br/>"; }
It would be really nice if the meta values are displayed in the same order as I queried but they are positioned as first description, category and Author. Is there any other way I can query database so that I can get values in that order which is, first Author, Category, Description. Thank you
Add
to your query text. Like this:
I strongly suggest that you thwart SQL injection by avoiding the inclusion of “unsafe” strings in your SQL text, e.g.
It just happens that you listed the search terms in alphabetical order, which makes the ORDER BY expression pretty simple.
If you needed them in some other order, you could use a different expression.
We also note that your query might return any number of values for each key (given that we don’t see a unique constraint on (post_id, meta_key).
It’s possible that the query could return zero, one or more values for Author, Category and Excerpt, and you have no way of knowing which is which.
My tendency here would be to include meta_key in the result set as well. Then my code would be able to tell that a particular
meta_valuewas for a particularmeta_key.