Solved

PHP/MySQL UNION statement question

Posted on 2011-09-08
3
296 Views
Last Modified: 2012-06-21
I am trying to use a union statement to select two values from two different tables. When i run it as an SQL it works fine but when I try to use it with php it only returns the first part of the query. here is the code below. I have also tried (statement a) UNION (statement b) like the description in the manual.  thanks for any help with this.

B
$query = "SELECT description FROM pages WHERE `page_id` = 'index' UNION SELECT age FROM names WHERE `name` = 'brian'";
$results = mysql_query($query);
$info = mysql_fetch_array($results);


$descrip = $info['description'];
$age = $info['age'];


echo $descrip . "-" . $age . "<br>";

Open in new window

0
Comment
Question by:jaxbrian
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 36504534
this is normal, because UNION will result in 2 rows, not "append" the columns.

you might want this:
query = "SELECT p.description , n.age FROM pages p, names n
WHERE p.`page_id` = 'index' and n.`name` = 'brian'";

$results = mysql_query($query);
$info = mysql_fetch_array($results);


$descrip = $info['description'];
$age = $info['age'];


echo $descrip . "-" . $age . "<br>";

Open in new window

0
 
LVL 2

Author Comment

by:jaxbrian
ID: 36504873
That worked, I have no idea why. lol. I am goign to read more on it now and see whats going on.

thank you for your help
0
 
LVL 2

Author Closing Comment

by:jaxbrian
ID: 36504876
thank you
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

730 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