Solved

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

Posted on 2014-12-26
5
207 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:SimonAdept
SimonAdept 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

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
The viewer will learn how to dynamically set the form action using jQuery.
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.

708 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

12 Experts available now in Live!

Get 1:1 Help Now