Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2014-12-26
5
Medium Priority
?
285 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
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

650 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