1

I want so when I load the page, it will start getting 5 rows(mysql query) every second from the mysql(with a cool fade-in effect if possible).

I'm using this code to show a list of all married players and their partners, and my database has many partners and I'm using an API to convert their UUID to their username, which takes long to load & convert, so I need to turn it into async.

I'm still learning PHP btw!

And finally, here's the code:

$mysql = mysql_connect('localhost', 'root', '');
mysql_select_db("marry",$mysql);
$result = mysql_query("SELECT player1,player2 FROM marriage_marriages");
$rows   = mysql_num_rows($result);


if ($rows) {
echo '<div class="col-lg-12"><table class="table"> <h2>Married Players</h2><thead><tr><th>User 1</th><th>User 2</th></tr></thead><tbody>';

while ($row = mysql_fetch_assoc($result)) {
echo '<tr>';


echo '<td>'. file_get_contents("http://api.mcusername.net/uuidtoplayer/".str_replace('-', '', '' . $row["player1"] . '')) .'</td>'; #user1


echo '<td>'. file_get_contents("http://api.mcusername.net/uuidtoplayer/".str_replace('-', '', '' . $row["player2"] . '')) .'</td>'; #user1
echo '</tr>';
}
echo '</tbody></table></div>';
  • Why did you write this code with mysql functions. Didn't you notice the big coloured warning in the [docs](http://php.net/manual/en/function.mysql-query.php)? – trincot Feb 27 '16 at 17:34
  • Sorry no. I'm very new to PHP and I've been learning a lot. And I'm using MySQL for my database with phpmyadmin. – DevilCraft Support Feb 27 '16 at 17:36
  • I am not talking about MySql as a database (that is fine!), but about the "mysql_....." functions which are old, deprecated, not maintained and not supported in PHP7. Use mysqli or PDO instead. – trincot Feb 27 '16 at 17:38
  • Alright. I'm currently converting my code from mysql to mysqli's code functions. Though I don't see a difference and I don't think it will resolve my async. But thanks for your help. – DevilCraft Support Feb 27 '16 at 17:41

1 Answers1

2

Instead of trying to make this asynchronous, consider that you can save a lot of time if you would store the player names in your database as soon as you insert values into it.

I would suggest you create a players table, which will have uuid as key and name as a second column.

Every time you insert a new player into that table, do the look-up as you have it now for a given uuid value:

$name = file_get_contents("http://api.mcusername.net/uuidtoplayer/".str_replace('-', '', '' . $uuid . ''));

Then complete the insert with those two values (I assume mysqli functions):

$stmt = $mysqli->prepare("INSERT INTO players (uuid, name) VALUES (?, ?)");
$stmt->exec($uuid, $name);

If you do this for all players, then your query on marriages would be:

SELECT      a.name, b.name
FROM        marriages m
INNER JOIN  players a
         ON m.player1 = a.uuid
INNER JOIN  players b
         ON m.player2 = b.uuid

And now you don't need to call those time consuming name lookups when building the table.

This will yield a tremendous performance improvement.

trincot
  • 317,000
  • 35
  • 244
  • 286
  • Hi, I would like to know if its possible to make async calls in PHP? – Nidhin David Feb 27 '16 at 17:50
  • 1
    @NidhinDavid, see [this question](http://stackoverflow.com/questions/13846192/php-threading-call-to-a-php-function-asynchronously). But remember that the browser is actually waiting for the PHP response, and will not allow the user to interact until PHP has finished. It is better to look for solutions where JavaScript makes these asynchronous calls with AJAX. That is how it's done. – trincot Feb 27 '16 at 17:54