Part 1: Importing a Table into MySQL

In order to have a high scores table, we need somewhere to store the data. This is where the MySQL database comes into play. For ease of use, I have attached a sample MySQL data table that you can download and import into your MySQL database. This sample data table is the database that I currently use for the flash game you just played.

  1. Click here to download the sample MySQL table. After downloading the file, you must import it into your own MySQL database before it can be used. This can be done through your web host and using phpMyAdmin. Import the table, and remember the name of the database you saved it in.
  2. Before we move on, we should check that the table was imported correctly. Copy the following code, and save it as a php file.
    <?php
     
    //////////  FILL IN THIS INFORMATION HERE! ////////////
     
      $host = "localhost"; //hostname is usually localhost by default
      $user = ""; //insert the name of the user here
      $pass = "";  //insert the password here
      $database = "";  //insert name of database wherein table was exported
      $table = "flashScores";  //insert the name of the table
     
    ////////////////////////////////////////////////////////
     
      // Connects to the database server
      // outputs an error message is it was unsuccessful
      $dbcnx = @mysql_connect($host,$user, $pass);
     
      if (!$dbcnx) {
        echo( "<P>Unable to connect to the database server at this time.</P>" );
        exit();
      }
     
      // Selects the specified database
      if (! @mysql_select_db($database) ) {
        echo( "Unable to find database" );
        exit();
      }
     
      echo("Here are all the flash scores in our database: <p>");
     
      // Request all the data from the table
      $result = mysql_query("SELECT * FROM {$table}");
      if (!$result) {
        echo("<P>Error performing query: mysql_error() </P>");
        exit();
      }
     
      // Display the first three records
      for($x=0;$x <3;$x++)
    	{  	
    	  $row = mysql_fetch_array($result);
              echo "Name : {$row["name"]} <br>" .
             "Final Time : {$row["finalTime"]} <br><br>";
      	}
     
    ?>
  3. Before the php script will work, you have to fill in some information: your host name, user name, password, database name, and table name. Your host name is given at the phpMyAdmin main page. Click here to see some examples if you don’t know your host name. Once you have that filled out, save the code as a php file and upload it onto your web server.
  4. Now, navigate to wherever you saved the php file on your web host. The following should appear in your browser if you filled in all the information and imported the data correctly:

    Here are all the flash scores in our database:
    Name : DexOwnsTyvm
    Final Time : 4.54

    Name : Rawr!
    Final Time : 5.02

    Name : Pwned
    Final Time : 5.13

  5. If the information above doesn’t appear, then that means that you have either imported the table incorrectly, did not assign the correct privileges to the user, or your information in the php file is incorrect. I recommend you go back and fix things before proceeding any further.
  6. If you want to learn how to create the table from scratch, I have written a quick tutorial on how to create a database and table within MySQL, which can be found here. But, I would recommend just using the table that I provided for now, as it is already populated with data and will make this tutorial easier to follow. Later on, you can create your own table that conforms to the application you are developing.

Pages: 1 2 3 4 5