user1761494
user1761494

Reputation: 125

PHP open connection & close only once

I have created a highscore script (takes data from a java game).

So this is the script I used to take the value for each row, for example this row will be called 'player name':

                <td style="padding:3px;" align="center">
                <?php
                    $con = mysql_connect(MYSQL_HOST, MYSQL_USERNAME, MYSQL_PASSWORD);
                    if (!$con)
                      {
                      die('Could not connect: ' . mysql_error());
                      }

                    mysql_select_db(MYSQL_DATABASE, $con);

                    $result = mysql_query("SELECT * FROM high ORDER BY Runecraftlvl DESC LIMIT 20");

                    while($row = mysql_fetch_array($result))
                      {
                      echo $row['playerName'];
                      echo "<br />";
                      }

                    mysql_close($con);
                ?>
                </td>

And I have 4 more scripts like this under each other. How do I open just one connection instead of open & closing connection everytime?

Thanks!

Upvotes: 1

Views: 183

Answers (1)

datasage
datasage

Reputation: 19563

Generally you wont need to close a connection as that will happen automatically at the end of the script.

The best way to open the connection is to put the connection code into a bootstrap file that will be included in each script.

Upvotes: 3

Related Questions