Solved

php sql server, left join, join query retrieving variables

Posted on 2014-12-06
7
121 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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Php logic to add to date card 9 37
Length of for loop to be dynamic 2 31
How can I find this data? 3 23
SQL parsing XML works but want to do it another way 4 16
These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
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.

680 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