Reader
  • Home
  • About
  • Deals
  • Services
  • Contact

MySQL Multi Query Function: An alternative to mysqli’s multi_query()

Last updated on January 9th, 2016 by Gabriel Livan

Performing multiple queries at once can be an effective and fast process using less code without any extra calls to the database. mysqli::multi_query can help you do that by executing one or multiple queries which are concatenated by a semicolon. However, not all servers have mysqli installed and when you also sell a product that requires a database import during the installation wizard you want to make sure that even the servers without mysqli will make the import successfully.

After I did some research I found a function that met my expectations and I’d like to share it with you:

function multiQuery($queryBlock, $delimiter = ';') {
	$inString = false;
	$escChar = false;
	$sql = '';
	$stringChar = '';
	$queryLine = array();
	$sqlRows = explode ( "\n", $queryBlock );
	$delimiterLen = strlen ( $delimiter );
	do {
		$sqlRow = current ( $sqlRows ) . "\n";
		$sqlRowLen = strlen ( $sqlRow );
		for ( $i = 0; $i < $sqlRowLen; $i   ) {
			if ( ( substr ( ltrim ( $sqlRow ), $i, 2 ) === '--' || substr ( ltrim ( $sqlRow ), $i, 1 ) === '#' ) && !$inString ) {
				break;
			}
			$znak = substr ( $sqlRow, $i, 1 );
			if ( $znak === '\'' || $znak === '"' ) {
				if ( $inString ) {
					if ( !$escChar && $znak === $stringChar ) {
						$inString = false;
					}
				}
				else {
					$stringChar = $znak;
					$inString = true;
				}
			}
			if ( $znak === '\\' && substr ( $sqlRow, $i - 1, 2 ) !== '\\\\' ) {
				$escChar = !$escChar;
			}
			else {
				$escChar = false;
			}
			if ( substr ( $sqlRow, $i, $delimiterLen ) === $delimiter ) {
				if ( !$inString ) {
					$sql = trim ( $sql );
					$delimiterMatch = array();
					if ( preg_match ( '/^DELIMITER[[:space:]]*([^[:space:]] )$/i', $sql, $delimiterMatch ) ) {
						$delimiter = $delimiterMatch [1];
						$delimiterLen = strlen ( $delimiter );
					}
					else {
						$queryLine [] = $sql;
					}
					$sql = '';
					continue;
				}
			}
			$sql .= $znak;
		}
	} while ( next( $sqlRows ) !== false );

	return $queryLine;
}

What does it do?

It splits the SQL block code into multiple queries that are later executed using mysql_query(). For example you have the following queries:

SELECT CURRENT_USER();
SELECT Name FROM City ORDER BY ID LIMIT 20, 5;

The function will create an array with 2 values, each containing the query that should be executed. Example:

$multiple_queries = 'SELECT CURRENT_USER();
SELECT Name FROM City ORDER BY ID LIMIT 20, 5;';

$sql_queries = multiQuery($multiple_queries);

foreach($sql_queries as $sql_query) {
    // Execute each query
    mysql_query($sql_query) or exit('SQL: '.$sql_query.', Error: '.mysql_error());
}

Note: If you are planning to import large block of SQL code in your projects I suggest you to setup mysqli on your server and use mysqli::multi_query as it is much faster. If you have a commercial app and you need to make sure you won’t get complaints from customers that can’t run a script because they don’t have mysqli installed, you can use the alternative solution.

Source: http://www.php4every1.com/tutorials/multi-query-function/

Filed Under: jQuery

About Me

Gabriel LivanHey, there! I'm Gabriel Livan, Web Developer, Blogger and Internet Marketer. I love making fast, secure and scalable web applications, running and travelling around the world. I blog about WordPress, site speed & security and quality tools for web professionals. Read more

What are you looking for?

Categories

  • AJAX (22)
  • Deals (10)
  • Design (30)
  • eBooks (2)
  • Flash (19)
  • Freebies (11)
  • General (62)
  • Giveaways (1)
  • Graphics (27)
  • HTML & CSS (35)
    • Frameworks (9)
  • JavaScript (160)
    • Frameworks (8)
    • jQuery (89)
    • MooTools (14)
  • Page Loading Speed (2)
  • Photoshop (3)
  • PHP (113)
  • Reviews (8)
  • SEO (6)
  • Statistics (2)
  • Web Tools (13)
  • WordPress (27)
    • Plugins (15)
    • Snippets (4)
    • Tools (2)
    • Tutorials (6)

Copyright © 2026 · BitRepository.com - All Rights Reserved

  • Home
  • About
  • Contact