Solved

PHP PDO foreach Statement

Posted on 2014-04-24
3
711 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
  • 2
3 Comments
 
LVL 52

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 52

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Amazon Redshift 2 24
unset shopping cart session 15 29
How to convert my query to the proper format? 5 15
append to an ms access field 6 16
CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

930 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now