Solved

PHP PDO foreach Statement

Posted on 2014-04-24
3
753 Views
Last Modified: 2014-04-25
I have a PHP PDO Query that runs almost 100%, however, I have one issue I'm not sure how to fix:  
I am querying a Wordpress DB and have used JOIN to go through all of the appropriate tables to get the desired results:
The query:
	$user_id = get_current_user_id();


	$sql = "SELECT
    		p1.ID AS ID,
    		pm1.meta_value AS customer_id,
			oi1.order_item_id AS order_item_id,
			om1.meta_value AS first_name,
			om2.meta_value AS last_name,
			om3.meta_value AS mailing_address
			FROM wp_posts p1
			JOIN wp_postmeta pm1 ON (pm1.post_id = p1.ID AND pm1.meta_key = '_customer_user')
			JOIN wp_woocommerce_order_items oi1 ON (oi1.order_id = p1.ID)
			JOIN wp_woocommerce_order_itemmeta om1 ON (om1.order_item_id = oi1.order_item_id AND om1.meta_key = 'Applicant Information - First Name')
			JOIN wp_woocommerce_order_itemmeta om2 ON (om2.order_item_id = oi1.order_item_id AND om2.meta_key = 'Applicant Information - Last Name')
			JOIN wp_woocommerce_order_itemmeta om3 ON (om3.order_item_id = oi1.order_item_id AND om3.meta_key = 'Applicant Information - Mailing Address')
			WHERE p1.post_type = 'shop_order'";
	

	
	$customer = $pdo->prepare($sql);
 
	// EXECUTE QUERY
	try {
    	$customer->execute();
		$result = $customer->fetchAll();

foreach ($result as $row) {
	$cid = $row->customer_id;
	$fn = $row->first_name;
    	$ln = $row->last_name;
	if ($user_id == $cid) {
    		echo $fn.' '.$ln;
	}
}

Open in new window


The code grabs all of the Order details from the DB table wp_woocommerce_order_itemmeta.  Lets say there are three orders in the DB, each for a different product.  I'm going to grab each product name and list it out, however, I don't want to list my name three times, I want to echo it out only once.
Does that make sense?  What other info can I offer you to help me fix this issue?
0
Comment
Question by:rgranlund
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 58

Expert Comment

by:Julian Hansen
ID: 40022082
Try putting DISTINCT in the field list

SELECT DISTINCT ...
0
 
LVL 7

Author Comment

by:rgranlund
ID: 40023464
Can you elaborate?
0
 
LVL 58

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 40023629
SELECT DISTINCT
    		p1.ID AS ID,
    		pm1.meta_value AS customer_id,
			oi1.order_item_id AS order_item_id,
			om1.meta_value AS first_name,
			om2.meta_value AS last_name,
			om3.meta_value AS mailing_address
			FROM wp_posts p1
			JOIN wp_postmeta pm1 ON (pm1.post_id = p1.ID AND pm1.meta_key = '_customer_user')
			JOIN wp_woocommerce_order_items oi1 ON (oi1.order_id = p1.ID)
			JOIN wp_woocommerce_order_itemmeta om1 ON (om1.order_item_id = oi1.order_item_id AND om1.meta_key = 'Applicant Information - First Name')
			JOIN wp_woocommerce_order_itemmeta om2 ON (om2.order_item_id = oi1.order_item_id AND om2.meta_key = 'Applicant Information - Last Name')
			JOIN wp_woocommerce_order_itemmeta om3 ON (om3.order_item_id = oi1.order_item_id AND om3.meta_key = 'Applicant Information - Mailing Address')
			WHERE p1.post_type = 'shop_order'

Open in new window

0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question