PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, August 31, 2022

[FIXED] How can I get all field values from a query with JOINed tables?

 August 31, 2022     mdb2, pear, php     No comments   

Issue

I have this elementary query:

SELECT d.description, o.code FROM order_positions AS o
LEFT JOIN article_descriptions AS d ON (o.article_id = d.article_id)
WHERE o.order_id = 1

and I'm using MDB2 from PEAR to execute it and read the return values.

But somehow the result array always contains fields from the order_positions table only!, i.e. the result array looks like this

row[code] = 'abc123'

while I want it to look like this

row[description] = 'my description'
row[code] = 'abc123'

I already tried the following:

  • Vary the order of the fields, i.e. code first, then description.
  • Vary the order of the joined tables.
  • Used full table names instead of aliases.
  • Used the "MySQL join" instead (SELECT FROM table1, table2 WHERE table1.id = table2.id)
  • Used aliases with and without AS.

Some other facts:

  • Executing this query in MySQL Query Browser works fine, all fields are returned.
  • The order_positions table seems to be preferred, no matter what. When joining with additional tables I still only get fields from this table.

Solution

OK, I found the cause:

Fields with NULL values are not added to the array. In my test scenario description was in fact null and hence was not available in the array.

I'll still keep this (embarrassing) question, just in case someone else has this problem in the future.

Facepalm http://www.scienceblogs.de/frischer-wind/picard-facepalm-thumb-512x409.jpg



Answered By - Daniel Rikowski
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing