Skip to content

Instantly share code, notes, and snippets.

@philip
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philip/68861c7beab1a5d77a90 to your computer and use it in GitHub Desktop.
Save philip/68861c7beab1a5d77a90 to your computer and use it in GitHub Desktop.
Copies a MySQL table a bunch of times, for testing
<?php
/*
* Copy a MySQL table n times as foo1, foo2, foo3, etc.
* TODO: add proper error handing, and docs
*/
$params = array(
'host' => '127.0.0.1',
'port' => 3306,
'socket' => '',
'user' => 'myUser',
'pass' => 'myPass',
'database' => 'myDb',
'original_table' => 'myTable',
'new_table_base' => 'ExampleTable',
'copies' => 100,
);
// Find a MySQL extension and create the tables
if ($ext = get_mysql_extension()) {
$func = 'use_' . $ext;
if (function_exists($func)) {
$func($params);
echo "Status: Created {$params['copies']} tables using the '$ext' MySQL extension";
}
}
// ext/mysqli
function use_mysqli($params) {
$conn = new mysqli($params['host'], $params['user'], $params['pass'], $params['database'], $params['port']);
if (!$conn) {
echo 'Could not connect to the database server' . mysqli_connect_error();
exit;
}
for ($i = 1; $i <= $params['copies']; $i++) {
$q1 = "CREATE TABLE {$params['new_table_base']}{$i} LIKE {$params['database']}.{$params['original_table']}";
$q2 = "INSERT {$params['new_table_base']}{$i} SELECT * FROM {$params['database']}.{$params['original_table']}";
$conn->query($q1);
$conn->query($q2);
}
$conn->close();
}
// ext/pdo_mysql
function use_pdo_mysql($params) {
$pdo = new PDO("mysql:host={$params['host']};dbname={$params['database']}", $params['user'], $params['pass']);
if (!$pdo) {
echo 'Could not connect to the database server' . $pdo->errorInfo();
exit;
}
for ($i = 1; $i <= $params['copies']; $i++) {
$q1 = "CREATE TABLE {$params['new_table_base']}{$i} LIKE {$params['database']}.{$params['original_table']}";
$q2 = "INSERT {$params['new_table_base']}{$i} SELECT * FROM {$params['database']}.{$params['original_table']}";
$pdo->query($q1);
$pdo->query($q2);
}
}
// deprecated ext/mysqli
function use_mysql($params) {
$conn = mysql_connect("{$params['host']}:{$params['port']}", $params['user'], $params['pass']);
if (!$conn) {
echo 'Could not connect to the database server' . mysql_error();
exit;
}
if (!mysql_select_db($params['database'])) {
echo "Could not select database {$params['database']}";
exit;
}
for ($i = 1; $i <= $params['copies']; $i++) {
$q1 = "CREATE TABLE {$params['new_table_base']}{$i} LIKE {$params['database']}.{$params['original_table']}";
$q2 = "INSERT {$params['new_table_base']}{$i} SELECT * FROM {$params['database']}.{$params['original_table']}";
mysql_query($q1);
mysql_query($q2);
}
mysql_close($conn);
}
// Find available MySQL extensions or return false
function get_mysql_extension() {
$exts = get_loaded_extensions();
$tests = array('mysqli', 'pdo_mysql', 'mysql');
foreach ($tests as $test) {
if (in_array($test, $exts)) {
return $test;
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment