* Copyright (c) 2003 Nextra Slovakia ISP, http://www.nextra.sk/ * Copyright (c) 2006 Platon Group, http://platon.sk/ * All rights reserved. * * Changelog: * 12/08/2003 - created * 26/08/2003 - added Content-type header * 12/07/2006 - enhanced, moved to Platon Group * */ /* $Id: mysql-test.php,v 1.6 2006/07/13 07:55:07 nepto Exp $ */ /* $Platon: */ // // Variables // $silent = strlen($_SERVER['QUERY_STRING']) > 0; $db_host = 'mysql.vpn.platon.sk'; $db_user = 'your_username'; $db_pass = 'your_password'; $db_name = $db_user; // when DB name is the same as DB user //$db_name = 'test'; $db_table = 'PlatonTestTable%d'; // // notice() // function notice($str) { global $silent; if ($silent) return 0; return printf('%-40s', $str) . ' ...'; } function notice_end($str) { global $silent; if ($silent) return 0; return print "$str\n"; } // // HTTP header // header('Content-type: text/plain'); // // Statup // if (! $silent) { echo "\n",'mysql-test.php - MySQL customer\'s database general test',"\n"; echo 'Developed by Ondrej Jombik ',"\n"; echo 'Copyright (c) 2006 Platon Group, http://platon.sk/',"\n"; echo 'All rights reserved.',"\n\n"; } // // Database connection // notice('Connecting to server'); if (($db_handle = @mysql_connect($db_host, $db_user, $db_pass)) == FALSE) { echo "ERROR\n\n"; die('mysql_connect() failure: ' . mysql_error() . "\n"); } notice_end('OK'); // // Database selection // notice('Selecting database'); if (@mysql_select_db($db_name) == FALSE) { echo "ERROR\n\n"; die('mysql_select_db() failure: ' . mysql_error() . "\n"); } notice_end('OK'); // // Table list // notice('Retrieving table list'); $query = 'SHOW TABLES'; if (($result = @mysql_query($query, $db_handle)) == FALSE) { echo "ERROR\n\n"; die('mysql_query() failure: ' . mysql_error() . "\n"); } for ($tables = array(); $row = @mysql_fetch_array($result, MYSQL_NUM); ) { $tables[] = $row[0]; } notice_end(sprintf('OK: %d tables', count($tables))); // // Composing table name // notice('Composing temporary table name'); for ($i = 0; ; $i++) { $table = sprintf($db_table, $i); if (! in_array($table, $tables)) { break; } if ($i > 1000) { echo "ERROR\n\n"; die("unable to find available table name\n"); } } notice_end(sprintf('OK: %s', $table)); // // Creating table // notice('Creating table'); $query = 'CREATE TABLE '.$table.' ( id INT NOT NULL AUTO_INCREMENT, dt DATETIME, name VARCHAR(255), data TEXT, PRIMARY KEY (id))'; if (@mysql_query($query, $db_handle) == FALSE) { echo "ERROR\n\n"; die('mysql_query() failure: ' . mysql_error() . "\n"); } notice_end('OK'); // // Inserting data // notice('Inserting data'); $query = sprintf('INSERT INTO %s' .' (id, dt, name, data) VALUES (NULL, NOW(), "%s", "%s")', $table, 'name', 'data'); for ($i = 0; $i < 10; $i++) { if (@mysql_query($query, $db_handle) == FALSE) { echo "ERROR\n\n"; die('mysql_query() failure: ' . mysql_error() . "\n"); } } notice_end(sprintf('OK: %d records', $i)); // // Selecting all data // notice('Selecting all data'); $query = sprintf('SELECT * FROM %s', $table); if (($result = @mysql_query($query, $db_handle)) == FALSE) { echo "ERROR\n\n"; die('mysql_query() failure: ' . mysql_error() . "\n"); } for ($i = 0; $row = @mysql_fetch_array($result, MYSQL_NUM); $i++); notice_end(sprintf('OK: %d records', $i)); // // Deleting all data // notice('Deleting data'); $query = 'DELETE FROM '.$table.' WHERE 1 = 1'; if (($result = @mysql_query($query, $db_handle)) == FALSE) { echo "ERROR\n\n"; die('mysql_query() failure: ' . mysql_error() . "\n"); } notice_end(sprintf('OK: %d records', intval(@mysql_affected_rows($db_handle)))); // // Droping table // notice('Droping table'); $query = 'DROP TABLE '.$table; if (@mysql_query($query, $db_handle) == FALSE) { echo "ERROR\n\n"; die('mysql_query() failure: ' . mysql_error() . "\n"); } notice_end('OK'); // // Database disconnection // notice('Disconnecting from server'); if (@mysql_close($db_handle) == FALSE) { echo "ERROR\n\n"; die('mysql_connect() failure: ' . mysql_error() . "\n"); } notice_end('OK'); // // End // if ($silent) { echo "OK\n"; } ?>