Run Multiple MySQL Queries in a Single Function

If you want to be able to run multiple queries in a single function call, for example doing the classic drop table blah; create table blah; then you might like this function.

The use case is for things like database migration systems which you might copy and paste chunks of SQL including multiple queries from things like phpMyAdmin

/**
 * Run multiple queries passed in as a single string
 * This is optimised for copying and pasting from phpMyAdmin
 * 
 * Handy for things like database migration systems
 * 
 * @param string $sql  multiple queries terminated with ; and a new line
 */
function multiQuery($sql)
{
    $sqls = preg_split('%;$%m', trim($sql));
    foreach ($sqls as $q) {
        if (empty($q)) {
            continue;
        }
        mysql_query($q); //suggest you replace this with your custom query function or if not throw in some extra error checking at least
    }
}

Tags: mysqlphptablemigrationsqldatabasephpmyadminquerycreatemultiquerydrop