Solved

Transitioning to PDO/MySQL.  Need help  with SELECT statement with LEFT JOIN

Posted on 2014-12-26
5
217 Views
Last Modified: 2014-12-28
I need assistance with a left join on a a SELECT statement.  I am not getting any errors, however, I am not getting any results either...



$conn = new PDO("mysql:host=$host;dbname=$db;charset=$charset", $user, $pass);

$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// Establish a MySQL connection
	
	$query2 = $conn->prepare('SELECT * FROM `product`
			LEFT JOIN `product_description`
			USING (product_id)
			LEFT JOIN url_alias
			USING (query)');
	
    while ($row2 = $query2->fetch(PDO::FETCH_ASSOC))
    {
		
	 echo $row2['product_id']."  ".$row2['description']."  ".$row2['query'];
	  
	}	

Open in new window

0
Comment
Question by:lawrence_dev
5 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 40519134
If you run the query direct in PHPMyAdmin or similar do you get any errors or results
Hard to debug without knowing the tables, but it looks OK.
0
 
LVL 18

Assisted Solution

by:Simon
Simon earned 20 total points
ID: 40519146
For multiple outer joins (e.g. LEFT JOIN) I'd use the slightly more verbose ON syntax to join the table rather than USING. It's not clear from your example which tables contain the key column 'query'.
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 30 total points
ID: 40519149
Without delving too deeply into it, I can tell you that any SELECT statement that works in the MySQL extension will work the same way in the MySQLi or PDO extensions.  The only difference will be in the way the variables are injected into the query string.  MySQL is going to be the same under the covers of any PHP extension.

You may want to use try / catch blocks so you can visualize any error messages.  These messages will be useful for your debugging.
0
 
LVL 58

Accepted Solution

by:
Gary earned 450 total points
ID: 40520094
Everyone missed the pink elephant in the room

You are preparing the statement but never executing it

	$query2 = $conn->prepare('SELECT * FROM `product`...

Open in new window



Code fixed:
	$query2 = $conn->query('SELECT * FROM `product`
			LEFT JOIN `product_description`
			USING (product_id)
			LEFT JOIN url_alias
			USING (query)');

    while ($row2 = $query2->fetch(PDO::FETCH_ASSOC))
    {
		
	 echo $row2['product_id']."  ".$row2['description']."  ".$row2['query'];
	  
	}	

Open in new window


When doing a query where there is no external input there is no need to use prepare - just execute the query immediately
0
 

Author Closing Comment

by:lawrence_dev
ID: 40520553
Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

863 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

20 Experts available now in Live!

Get 1:1 Help Now