CSC220 fillTable.php

From dftwiki3
Revision as of 08:17, 20 October 2010 by Thiebaut (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

--D. Thiebaut 13:17, 20 October 2010 (UTC)


A simple Php program that will stored 1,000,000 records in a table (users) in a database (220a_example5).

// fillTable.php
// D. Thiebaut


//----------------------------------------------------------
// Globals.  Enter your user information here
//----------------------------------------------------------
$user        ="root";   // EDIT THIS LINE!
$passwd      = "xxxx";       // EDIT THIS LINE TOO!
$host        ="xgridmac.dyndns.org";
$database    = "220a_example5";   // EDIT THIS LINE AS WELL!


//----------------------------------------------------------

//--- connect ---
mysql_connect( $host, $user, $passwd ) or die( "Unable to connect to db\n\n" );
print "==> Connected to database\n";

//--- select database ---
$result = mysql_select_db($database);


//--- display result ---
$N = 1000000;
for ( $i=0; $i < $N; $i++ ) {
  $qty  = rand( ); // random number
  $name =  strval( rand( ) ); // random number as a string

   mysql_query( "INSERT INTO `220a_example5`.`users` (`Name`, `Qty`) "
                              . " VALUES ( '$name', '$qty' )"  );

  //--- give feedback to user ---
  if ( $i % 1000 == 0 ) {
    print $i . "\n";
  }
}

//--- done.  close connection ---
mysql_close();

?>