Solved

php sql server, left join, join query retrieving variables

Posted on 2014-12-06
7
111 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 108

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 108

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 108

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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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 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 …

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now