?
Solved

PHP/MySQL UNION statement question

Posted on 2011-09-08
3
Medium Priority
?
305 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

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