View Full Version : Showing mysql in table
d-machine
08-08-2008, 12:00 AM
Hi,
I have 2 tables;
1) name, last name.
2) last name, shirt-color.
I want to print (as html) a table that shows:
name | last name | shirt-color (asuming each name has other last name)
I just don't know how to print a table which combines 2 tables.
Any ideas? ;)
Try something like:
SELECT
table1.name,
table1.lastname,
table2.shirtcolor
FROM
table1,
table2
WHERE
table1.lastname = table2.lastname;
Take a look at this, as well. (http://dev.mysql.com/doc/refman/5.0/en/select.html) You may find it helpful. It starts with basic queries, which I am sure you already know, and goes up to more complex ones.
lord22
08-08-2008, 10:02 AM
Hi Jas and d-machine I've tried to do it too (for practice),
That's what I've tried to do:
$result = mysql_query("SELECT mytable1.name, mytable1.lastname, mytable2.shirtcolor FROM mytable1, mytable2
WHERE mytable1.lastname = mytable2.lastname;");
echo "<table border='1'>";
echo "<tr> <th>name</th> <th>lastname</th> <th>shirtcolor</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo $row['mytable1.name'];
echo "</td><td>";
echo $row['mytable1.lastname'];
echo "</td><td>";
echo $row['mytable2.shirtcolor'];
echo "</td></tr>";
}
echo "</table>";
for some reason it printed me an empty table (with the correct size of rows and columns).
d-machine
08-08-2008, 04:13 PM
Thank you lord22 and Jas,
I've tried your both Ideas and actually got stuck exactly in the same problem as lord22..
allahverdi
08-09-2008, 06:31 AM
Change this:
$result = mysql_query("SELECT mytable1.name, mytable1.lastname, mytable2.shirtcolor FROM mytable1, mytable2
WHERE mytable1.lastname = mytable2.lastname;");
To this:
$result = mysql_query("SELECT mytable1.name, mytable1.lastname, mytable2.shirtcolor FROM mytable1, mytable2
WHERE mytable1.lastname = mytable2.lastname;") or die(mysql_error());
And look at the error.
Nightfire
08-09-2008, 10:27 AM
Also try removing the semi-colon at the end of the query
mytable2.lastname;");
d-machine
08-09-2008, 09:23 PM
Thank you, I've fixed my errors, and now it works fine!
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.