1

When running a SQL query I am getting a strange NULL result. Here is the query:

SELECT * FROM plan LEFT JOIN bill ON plan.planID = bill.planID AND bill.typeID =3 WHERE bill.billID IS NULL 

I am running the query in PHP using a PDO object. All of the results are coming out as such:

Array ( [planID] => [0] => 7 [clientID] => 4 ...)

The problem lies within the first value returned. As you can see PlanID is NULL but when the same value is pulled with the numeric key, it appears properly as "7". I have tried to address the issue by adding PDO::FETCH_ASSOC to the query code, but I get the following result:

Array ( [planID] => [clientID] => 4 ...)

I do not understand how or why this is occurring. Any help is much appreciated!

2
  • And you're 100% certain you have no null values in your plan table? Commented Jun 5, 2014 at 20:24
  • Yes, I have pulled up several lines on the back end that match the lines pulled by my PHP and they have the proper PlanID shown in the [0]=> 7 Commented Jun 5, 2014 at 20:26

1 Answer 1

3

I'm not 100% sure this is your problem, but when you say:

select *
from . . .

it is returning all columns from all tables. This means that there are two columns called planId in the results, one from plan and one from bill. The one from bill is NULL, assuming that the purpose of the where clause is to find non-matches. That may be the one that you are seeing.

The solution is to explicitly choose the specific columns that you want. Perhaps:

select plan.planId
from . . .
Sign up to request clarification or add additional context in comments.

2 Comments

I'm not sure if it's legal in mysql, but in oracle you can do table.* to pull from a specific table.
Good Point, doing some testing now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.