mysqli multi_query

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
ThaJock
Forum Newbie
Posts: 11
Joined: Tue Nov 04, 2008 10:15 pm

mysqli multi_query

Post by ThaJock »

In order to avoid having to execute multiple queries with mysql_query I did some reading and found that with mysqli_multi_query you can execute multiple queries in one call (provided your sql syntax is correct). It works great. I can even iterate through each result. However, I found that on CREATE and DELETE statements it does not return any result. So how do I know if the table was deleted or created? Hear is the code.

First here is the sql code that I store in a file named install.sql:

Code: Select all

DROP TABLE IF EXISTS `doc_classes`;
CREATE TABLE `doc_classes` (
  `id` int(5) NOT NULL auto_increment,
  `class` varchar(100) NOT NULL,
  `package` varchar(100) NOT NULL,
  `inherentance` varchar(255) NOT NULL,
  `language_version` varchar(100) NOT NULL,
  `runtime_version` varchar(100) NOT NULL,
  `description` longtext NOT NULL,
  PRIMARY KEY  (`id`)
);
Here is the php code:

Code: Select all

<?php
$sql = new mysqli('localhost','root','root','DBName',8888);
$query = file_get_contents('install.sql');

$resource = $sql->multi_query($query);
if($resource){
	do{
		if($res = $sql->use_result()){
			while($row = $res->fetch_object()){
				print_r($row); echo "<br/><br/>";
			}
		}
	}
	while($sql->next_result());
}

?>
The script runs and the table is created but I am unable to test the result in my php code. If anyone knows of any was to test if the table is deleted or created I would greatly appreciate any advise.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: mysqli multi_query

Post by Jade »

Try inserting a value into the table you created. If the table creation failed your insert will fail.
Post Reply