?
Solved

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

Posted on 2014-12-26
5
Medium Priority
?
271 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
[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
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 80 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 120 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 1800 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

765 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