James Logan
James Logan

Reputation: 831

PHP function to compare data in two tables to incorporate in existing script

I need a PHP script that would pull data (player ID) from a mysql table (I know how to do it so far) AND THEN use that player ID (1-5 digit number) to find the corresponding player name in another table.

Basically I need to display which player posted which message on a website.

Here's the script:

<?php
include('mysql_connection.php');

$c = mysqlConnect();

$locale = $_GET['locale'];
$last_bulletins_id = $_GET['bulletins_id'];

sendQuery ("set character_set_results='utf8'"); 
sendQuery ("set collation_connection='utf8_general_ci'"); 

if(strcmp($locale,"en") != 0)
    $locale = "en";
$result = sendQuery("SELECT * FROM bulletins WHERE id > ".$last_bulletins_id." and locale = '".$locale."' ORDER BY id DESC LIMIT 10");
echo '<table width=\"100%\">';
while($row = mysql_fetch_array($result, MYSQL_NUM))
{
    echo '<tr><td width=\"100%\"><b>Author: </b></td></tr>';
    echo '<tr><td width=\"100%\"><b>Date: </b>'.$row[2].'</td></tr>';
    echo '<tr><td width=\"100%\">'.nl2br($row[4]).'</td></tr>';
    echo '<tr><td width=\"100%\"><hr style="height: 2px; border: none; background: #515151;"></td></tr>';
}
echo '</table>';

mysqlClose($c);
?>

However, the message rows do not have the player name. Only his ID. His name is held in another database. So I somehow have to pull the ID, use it to find the corresponding name in another table and display the corresponding name.

What function to use to do this?

Upvotes: 0

Views: 513

Answers (3)

Syed Osama
Syed Osama

Reputation: 133

1st DB: FDB ---> table 'user' -- > column 'user_id'

2nd DB: SDB ---> table 'user' -- > column 'user_id, user_name'

SELECT FDB.user.user_id, SDB.user.user_name 
FROM FDB.user, SDB.user
WEHRE FDB.user.user_id = SDB.user.user_id

Upvotes: 1

Anton Sementsov
Anton Sementsov

Reputation: 1246

Do JOIN in your mysql query with the table which ceep player name and Id

Upvotes: 0

Olaf Dietsche
Olaf Dietsche

Reputation: 74078

If the player name is in the same database and just another table, you can use a join for this

SELECT b.*, p.username
FROM bulletins b
join players p on p.user_id = b.user_id
WHERE b.id > $last_bulletins_id
      and b.locale = '$locale'
ORDER BY b.id DESC
LIMIT 10

Upvotes: 1

Related Questions