Jump to content

results from two db tables into one html table?


Chris.P

Recommended Posts

I have a query below that gets its results and puts them in a table. The current results are from two fields in a db table. I am wondering if it is possible to create a second query to get results from a different db table and echo them out in the same html table as the ones already there? Many thanks. :)

 

        <?php
include_once  'functions.php';
loginDetails();
session_start();
	$query = "SELECT * FROM users ORDER BY id";

// execute query 
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
// see if any rows were returned 
if (mysql_num_rows($result) > 0) { 
    // yes 
    // print them one after another 
    echo "<table id=drtable>";
    echo "<tr><td><h2>Band ID</h2></td>";
echo "<td><h2>Band Name</h2></td></tr>"; 

    
while($row = mysql_fetch_assoc($result)) { 
	echo "<tr>";
        echo "<td><h2><a href=profile.php?bandid=".$row['id'].">".$row['id']."</a></h2></td>";
        echo "<td><h2><a href=profile.php?bandid=".$row['id'].">".$row['bandname']."</a></h2></td>"; 
        echo "</tr>";
    } 
    echo "</table>"; 
} 
else { 
    // no 
    // print status message 
    echo "No bands found."; 
} 
  ?>

Cheers I did it with a MySQL join. It seems to be working although images are not showing properly as the image locations are all getting %3c/a added on after the file extension. Has me baffled as to why this is happening, any ideas?

 

Full query with output

 

        <?php
include_once  'functions.php';
loginDetails();
session_start();
	$query = "SELECT users.id, users.bandname, images.path ".
	"FROM users, images ".
	"WHERE users.id = images.id";
	//$query = "SELECT * FROM users ORDER BY id";

// execute query 
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
// see if any rows were returned 
if (mysql_num_rows($result) > 0) { 
    // yes 
    // print them one after another 
    echo "<table id=drtable>";
    echo "<tr><td><h2>Band Image</h2></td>";
echo "<td><h2>Band Name</h2></td>";
echo "<td><h2>Band ID</h2></td></tr>"; 

    
while($row = mysql_fetch_assoc($result)) { 
	echo "<tr>";
        echo "<td><h2><a href=profile.php?bandid=".$row['id'].">","<img src=".$row['path']."</a></h2></td>";
	echo "<td><h2><a href=profile.php?bandid=".$row['id'].">".$row['bandname']."</a></h2></td>";
        echo "<td><h2><a href=profile.php?bandid=".$row['id'].">".$row['id']."</a></h2></td>"; 
        echo "</tr>";
    } 
    echo "</table>"; 
} 
else { 
    // no 
    // print status message 
    echo "No bands found."; 
} 
  ?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.