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

81

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 class="page_speed_1922375399"></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?

779

Answer

Solution:

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
337

Answer

Solution:

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
16

Answer

Solution:

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

People are also looking for solutions to the problem: php - Paypal AngellEye DoExpressCheckout - data mismatch

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.