$shortopts .= "h:"; // Required value username $shortopts .= "d:"; // Required value password $shortopts .= "u:"; // Required value host $shortopts .= "p:"; // Required value database $longopts = array("file:", "create_table::", "dry_run::"); $options = getopt($shortopts, $longopts); $DBAllSet = FALSE; if (array_key_exists("h", $options) && array_key_exists("d", $options) && array_key_exists("u", $options)) { $DBAllSet = TRUE; } if (array_key_exists("dry_run", $options)) { dryRun($options); } elseif (array_key_exists("create_table", $options) && $DBAllSet) { createTable($options); } elseif (array_key_exists("file", $options) && $DBAllSet) { importIntoMySQL($options); } else { ?> This is a command line PHP script that import csv file into MySql. Usage: <?php echo $argv[0]; ?> <option>
function commandCheck($command) { global $servername, $username, $password; // Swicth case checking input argument switch ($command[1]) { case "--help": helpDirectives(); break; case "--file": $filename = $command[2]; if ($command[3] == "-u") { $username = $command[4]; } elseif ($command[3] == "-p") { $password = $command[4]; } elseif ($command[3] == "-h") { $servername = $command[4]; } else { exit("Invalid user or host argument\n"); } if ($command[5] == "-u") { $username = $command[6]; } elseif ($command[5] == "-p") { $password = $command[6]; } elseif ($command[5] == "-h") { $servername = $command[6]; } else { exit("Invalid user or host argument\n"); } if ($command[7] == "-u") { $username = $command[8]; } elseif ($command[7] == "-p") { $password = $command[8]; } elseif ($command[7] == "-h") { $servername = $command[8]; } else { exit("Invalid user or host argument\n"); } initDB(); insertFile($filename); break; case "--create_table": if ($command[2] == "-u") { $username = $command[3]; } elseif ($command[2] == "-p") { $password = $command[3]; } elseif ($command[2] == "-h") { $servername = $command[3]; } else { exit("Invalid user or host argument\n"); } if ($command[4] == "-u") { $username = $command[5]; } elseif ($command[4] == "-p") { $password = $command[5]; } elseif ($command[4] == "-h") { $servername = $command[5]; } else { exit("Invalid user or host argument\n"); } if ($command[6] == "-u") { $username = $command[7]; } elseif ($command[6] == "-p") { $password = $command[7]; } elseif ($command[6] == "-h") { $servername = $command[7]; } else { exit("Invalid user or host argument\n"); } initDB(); createTable(); break; case "--dry_run": $filename = $command[3]; if ($command[4] == "-u") { $username = $command[5]; } elseif ($command[4] == "-p") { $password = $command[5]; } elseif ($command[4] == "-h") { $servername = $command[5]; } else { exit("Invalid user or host argument\n"); } if ($command[6] == "-u") { $username = $command[7]; } elseif ($command[6] == "-p") { $password = $command[7]; } elseif ($command[6] == "-h") { $servername = $command[7]; } else { exit("Invalid user or host argument\n"); } if ($command[8] == "-u") { $username = $command[9]; } elseif ($command[8] == "-p") { $password = $command[9]; } elseif ($command[8] == "-h") { $servername = $command[9]; } else { exit("Invalid user or host argument\n"); } initDB(); dryRun($filename); break; default: echo "Invalid command entered\n"; } }