Hey guys,
I need some help with selecting 2 Tables from a MySQL database. Can anyone help.

Currently I am using this, and I want it to be able to select from another table called Admin and Display the Admins under admins in my table.

<?php

$host=""; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name=""; // Database name 
$tbl_name=""; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// select record from mysql 
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
?>

<table width="400px" border="2" cellspacing="3" cellpadding="5">
<tr>
<td><table width="400px" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td col="2"
</tr>
<tr>
<td align="left" bgcolor="#FFFFFF"><strong>Normal Users</strong></td>
<td align="left" bgcolor="#FFFFFF"><strong>Admin Users</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td bgcolor="#FFFFFF"><? echo $rows['Username']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['Username']; ?></td>
</tr>
<?
// close while loop 
}
// close connection; 
mysql_close();
?> 
</table>

Can someone Please help. Thanks in advance :)

Dani AI

Generated

— the goal is to show normal users in one column and admins in the other. is right that a second query/result is the simplest route, but the old mysql_* functions used in your snippet are deprecated and removed in recent PHP versions. A cleaner, safer approach is to run two queries with PDO (or mysqli), fetch each result set into an array, then iterate by index to print rows side‑by‑side. This keeps the logic simple and avoids reusing one result resource for two different lists.

Example pattern (PDO):

<?php
// create PDO, run two queries and fetch into arrays
$pdo = new PDO('mysql:host=localhost;dbname=your_db;charset=utf8mb4','user','pass',[
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
]);

$users  = $pdo->query("SELECT username FROM users ORDER BY username")->fetchAll();
$admins = $pdo->query("SELECT username FROM admins ORDER BY username")->fetchAll();

$max = max(count($users), count($admins));
echo '<table border="1"><tr><th>Normal Users</th><th>Admin Users</th></tr>';
for ($i = 0; $i < $max; $i++) {
    $u = isset($users[$i])  ? htmlspecialchars($users[$i]['username'])  : '';
    $a = isset($admins[$i]) ? htmlspecialchars($admins[$i]['username']) : '';
    echo "<tr><td>{$u}</td><td>{$a}</td></tr>";
}
echo '</table>';

Alternative: if users and admins are really just roles of the same entity, use a single query (UNION or a role column) and then group or split the rows in PHP. That can be simpler when you have one table with a role field.

Troubleshooting and cautions: verify exact column/table names, escape output with htmlspecialchars to avoid XSS, use prepared statements if any input filters are applied, and avoid fetchAll on extremely large tables (stream results or paginate). Upgrading from mysql_* to PDO/mysqli is important for compatibility and security.

If you want a separate read of another table, then define the select statement and use a mysql query with another variable as in:

$result2=mysql_query($sql);

In the subsequent code for that query, you will refer to $results instead of $result.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.