Solved

PHP/MySQL UNION statement question

Posted on 2011-09-08
3
269 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
  • 2
3 Comments
 
LVL 142

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

919 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now