MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 5th, 2013, 09:19 PM
agentk agentk is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 1 agentk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 40 sec
Reputation Power: 0
Red face Newbie Question - How to combine these queries?

I am trying to extract raw data from my WordPress database and have been successful is doing so, but I needed to use a different query every time. Could someone help me out on how to combine these queries into one query?

PHP Code:
<?php include('db.php'); $id=mysql_real_escape_string($_GET['id']); $sql=mysql_query("SELECT * FROM wp_posts WHERE ID = '$id'"); $count=mysql_num_rows($sql); $row=mysql_fetch_array($sql); $title=$row['post_title']; echo $titlemysql_close($sql); $sql=mysql_query("SELECT * FROM wp_postmeta WHERE post_id='$id' AND meta_key = 'post_type_value' "); $count=mysql_num_rows($sql); $row=mysql_fetch_array($sql); echo $row['meta_value']; mysql_close($sql); $sql=mysql_query("SELECT * FROM wp_postmeta WHERE post_id='$id' AND meta_key = 'mabp_description' "); $count=mysql_num_rows($sql); $row=mysql_fetch_array($sql); echo $row['meta_value']; mysql_close($sql); $sql=mysql_query("SELECT * FROM wp_postmeta WHERE post_id='$id' AND meta_key = 'mabp_thumbnail_url' "); $count=mysql_num_rows($sql); $row=mysql_fetch_array($sql); echo $row['meta_value']; mysql_close($sql); $sql=mysql_query("SELECT * FROM wp_postmeta WHERE post_id='$id' AND meta_key = 'mabp_swf_url' "); $count=mysql_num_rows($sql); $row=mysql_fetch_array($sql); echo $row['meta_value']; mysql_close($sql); $sql=mysql_query("SELECT * FROM wp_postmeta WHERE post_id='$id' AND meta_key = 'mabp_height' "); $count=mysql_num_rows($sql); $row=mysql_fetch_array($sql); echo $row['meta_value']; mysql_close($sql); $sql=mysql_query("SELECT * FROM wp_postmeta WHERE post_id='$id' AND meta_key = 'mabp_width' "); $count=mysql_num_rows($sql); $row=mysql_fetch_array($sql); echo $row['meta_value']; mysql_close($sql); ?>


Thanks! Much help is appreciated.

Reply With Quote
  #2  
Old January 6th, 2013, 12:39 AM
E-Oreo's Avatar
E-Oreo E-Oreo is offline
Lost in code
Click here for more information.
 
Join Date: Dec 2004
Posts: 7,939 E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)  Folding Points: 945 Folding Title: Novice Folder
Time spent in forums: 2 Months 9 h 12 m 42 sec
Reputation Power: 7053
You would use a left or inner join on it; the former for optional meta keys and the latter for required meta keys. For example:
Code:
SELECT
  wp_posts.post_title,
  post_type_meta.meta_value AS post_type_value,
  mabp_description_meta.meta_value AS mabp_description_value
FROM wp_posts
LEFT JOIN wp_postmeta AS post_type_meta ON
  post_type_meta.post_id = wp_posts.ID
  AND post_type_meta.meta_key = 'post_type_value'
LEFT JOIN wp_postmeta AS mabp_description_meta ON
  mabp_description_meta.post_id = wp_posts.ID
  AND mabp_description_meta.meta_key = 'mabp_description'
WHERE wp_posts.ID = '$id'


However, note that:
* The mysql_* PHP library is deprecated and mysqli or PDO should be used instead.
* You don't need to call mysql_num_rows unless you're actually going to use the value.
* mysql_close closes the connection to the MySQL server, which is not what you want to be doing after every query; you shouldn't be calling this in your script.
__________________
PHP FAQ
How to program a basic, secure login system using PHP

Quote:
Originally Posted by Spad
Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > Newbie Question - How to combine these queries?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap