Solved

PHP/MySQL UNION statement question

Posted on 2011-09-08
3
259 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
PHP and Soap 3 30
lastpass auto fill login form 5 25
How can I expand text with explaination 5 22
sql sentence 2 13
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

747 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

10 Experts available now in Live!

Get 1:1 Help Now