• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 155
  • Last Modified:

php sql server, left join, join query retrieving variables

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


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


Open in new window

but I am having now luck
  • 3
  • 3
1 Solution
Zsolt PribuszCommented:
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

Ray PaseurCommented:
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?

ZipbangAuthor Commented:
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.
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Ray PaseurCommented:
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:

Best of luck with your project, ~Ray
ZipbangAuthor Commented:

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.
Ray PaseurCommented:
link bait
Ha!  That might make sense if I get paid when you read my articles.  Like I said, "Best of luck..."
ZipbangAuthor Commented:
It's not about luck, but thanks
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now