Solved

php sql server, left join, join query retrieving variables

Posted on 2014-12-06
7
124 Views
Last Modified: 2014-12-12
A query with a join as such


$sql = "SELECT * FROM tableA
		LEFT JOIN tableB ON (tableA.customerid=tableB .Id)";
 
//---prepare query above
$stmt=sqlsrv_query( $conn, $sql, $params, array( "Scrollable" => 'keyset' ));

Open in new window


in a loop where this drives it

while ($row = sqlsrv_fetch_array($stmt)){ 

 

Open in new window


trying to pull the Id which both tableA and tableB have

  
$Id=$row[Id];

Open in new window

Gets only tableA's Id of course, I need both Ids

I am used to MySQL/PHP where I can specify as

$IdA=$row[tableA.Id];
$IdB=$row[tableB.Id];

Open in new window


but I am having now luck
0
Comment
Question by:Zipbang
[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
  • 3
  • 3
7 Comments
 
LVL 4

Accepted Solution

by:
Zsolt Pribusz earned 500 total points
ID: 40484946
Try to use alias for ID's.

For example:

$sql = "SELECT tableA.id as IdA, tableB.id as IdB, * FROM tableA
            LEFT JOIN tableB ON (tableA.customerid=tableB.Id)";

Then you can get IDs

$IdA=$row[IdA];
$IdB=$row[IdB];
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40484976
To understand why this is an antipractice, please read about the use of quotes in PHP.

$IdA=$row[IdA]; // What is the defined constant named IdA?

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_12241-Quotation-Marks-in-PHP.html
0
 

Author Comment

by:Zipbang
ID: 40492880
Ok, I have one suggestion and one comment that the suggestion is the antipractice.  

Zsolt's comment will solve the problem, but if it is the wrong way, please tell me the correct method to solve my problem.

thank you.
0
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40495423
The correct way is written in the article.  If you still have questions after reading the article, please post back and make reference to the part of the article that is unclear to you.  I will answer your questions and update the article to clarify it.

If you're new to PHP and want to get a good foundation for learning, this might help:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

Best of luck with your project, ~Ray
0
 

Author Comment

by:Zipbang
ID: 40496131
Ray,

I pay for this service for quick solutions.  While an article on the foundations of PHP is a good resource, it is not a solution.  In this context, it is simply link bait to get me to read your article.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40497437
link bait
Ha!  That might make sense if I get paid when you read my articles.  Like I said, "Best of luck..."
0
 

Author Comment

by:Zipbang
ID: 40497540
It's not about luck, but 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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

707 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