Skip to content

Instantly share code, notes, and snippets.

@cjs226
Created July 24, 2013 19:49
Show Gist options
  • Save cjs226/6073900 to your computer and use it in GitHub Desktop.
Save cjs226/6073900 to your computer and use it in GitHub Desktop.
Attempting to iterate over MySQL tables using PHP
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);
if (($argc < 2) || ($argc > 4)){
print "$argv[0] <application env>\n";
exit(1);
}
if (!defined('APPLICATION_ENV'))
define('APPLICATION_ENV', $argv[1]);
require_once 'bootstrap.php';
require_once 'Company/DataSourceUtil.php';
require_once 'Company/Graphite.php';
function getSqlResult($db, $sql) {
try {
$result = $db->fetchOne($sql);
} catch (Zend_Db_Exception $e) {
echo "SQL ERROR: ".$e->getMessage();
}
return $result;
}
$master = Zend_Registry::get('master');
//Date used for SQL queries
$now = strftime('%F');
$ts = strtotime($now);
$result = array();
foreach (DataSourceUtil::getAllPartitionIds() as $partitionId) {
$partitionDb = DataSourceUtil::getDataDb($partitionId);
$partitionDbName = DataSourceUtil::getDbName(DataSourceUtil::getPartitionDataSourceName($partitionId));
//Broken code starts here
$sql = "SHOW TABLES FROM $partitionDbName";
$tables = getSqlResult($partitionDb, $sql);
foreach ($tables as $table) {
echo "$partitionDbName $table";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment