1

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!

1
  • DO NOT CONTINUE TO USE mysql_* FUNCTIONS. This extension is old and deprecated and due to be removed from PHP. Use PDO or MYSQLI instead. Commented Jan 27, 2013 at 17:12

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.