Solved

php sql server, left join, join query retrieving variables

Posted on 2014-12-06
7
118 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 109

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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 109

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 109

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
The viewer will learn how to dynamically set the form action using jQuery.
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…

770 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