Re: mysql_query()

From: Date: Tue, 14 Aug 2001 13:59:00 +0000
Subject: Re: mysql_query()
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-62625@lists.php.net to get a copy of this message
I have made this a small script called SQL2PHP, --- <html> <head><title>SQL2PHP</title></head> <body> <pre> <? /* SQL2PHP v0.1 by elias_bachaalany@yahoo.com You may use this script as long as you credit the original author */ function parse_sql_stream($all) { $len = strlen($all); $lines = array(); $inquotes = false; $lastquote = ''; $lastch = "\xa"; $start = 0; $incomment1 = false; $incomment2 = false; $brokestr = ""; for ($i=0;$i<$len;$i++) { $ch = $all[$i]; // single line comment - ON if ( $ch == '#' && !$inquotes) { $incomment1 = true; continue; } // single line comment - OFF if ($incomment1 && ($ch == "\r" || $ch == "\n")) { $incomment1 = false; $start = $i; continue; } // multiline comment - ON if (!$inquotes && $lastch == '/' && $ch == '*') { $incomment2 = true; $brokestr = substr($all, $start, $i - $start - 1); continue; } // multiline comment - OFF if ($incomment2 && $ch == '/' && $lastch=='*') { $incomment2 = false; $start = $i+1; continue; } // inquote desicions if (($ch == '\'' || $ch=='"')) { if (!$inquotes) { $lastquote = $ch; $inquotes = true; } else $inquotes = ( ($lastch == "\\" && $ch == $lastquote) || ($ch != $lastquote) ); } // new statment decision if ($ch == ';' && !$inquotes && !$incomment1 && !$incomment2) { $lines[] = trim($brokestr . trim(substr($all, $start, $i - $start))); $brokestr = ""; $i++; // skip the ';' $start = $i; } // update last read character $lastch = $ch; } // for i return $lines; } function parse_sql_file($filename) { $fp = fopen($filename, 'rb'); $all = fread($fp, filesize($filename)); fclose($fp); return parse_sql_stream($all); } set_time_limit(0); mysql_connect('localhost', 'test', 'test'); $test = parse_sql_file('file3.sql'); for ($i=0; $i<count($test);$i++) { $result = mysql_query($test[$i]); if (!$result) { echo "Failed at line $i with query:\n{$test[$i]}\nMySql error:" . mysql_error() . "\n"; break; } } echo "done!\n"; ?> </pre> </body> </html> --- file3.sql can contain: --- DROP DATABASE t; CREATE DATABASE t; USE t; # # Table structure for table 'brands' # DROP TABLE /*!32200 IF EXISTS*/ brands; CREATE TABLE /*!32300 IF NOT EXISTS*/ brands ( id int(10) unsigned NOT NULL auto_increment, language tinyint(2) DEFAULT '1' , company varchar(50) , countryid int(10) unsigned , name varchar(50) NOT NULL DEFAULT '' , logo varchar(200) NOT NULL DEFAULT '' , description mediumtext NOT NULL DEFAULT '' , PRIMARY KEY (id) ); # # Dumping data for table 'brands' # INSERT INTO brands VALUES("122","1",NULL,"12","Hummer","",""); INSERT INTO brands VALUES("2","1",NULL,"12","Buick","",""); INSERT INTO brands VALUES("3","1",NULL,"12","GMC","",""); INSERT INTO brands VALUES("4","1",NULL,"12","Jeep","",""); INSERT INTO brands VALUES("5","1",NULL,"12","Ford Motor Company","",""); INSERT INTO brands VALUES("6","1",NULL,"12","Opel","",""); INSERT INTO brands VALUES("7","1",NULL,"12","Audi","",""); INSERT INTO brands VALUES("8","1",NULL,"12","BMW","",""); INSERT INTO brands VALUES("9","1",NULL,"12","Porche","",""); INSERT INTO brands VALUES("10","1",NULL,"12","Mercedes","",""); INSERT INTO brands VALUES("11","1",NULL,"12"," Infinity*","",""); INSERT INTO brands VALUES("12","1",NULL,"12","Isuzu","",""); INSERT INTO brands VALUES("13","1",NULL,"12","Subaru","",""); INSERT INTO brands VALUES("14","1",NULL,"12","Suzuki","",""); INSERT INTO brands VALUES("15","1",NULL,"12","Mazda","",""); INSERT INTO brands VALUES("16","1",NULL,"12","Mitsubishi Motors","",""); INSERT INTO brands VALUES("17","1",NULL,"12","Honda","",""); INSERT INTO brands VALUES("18","1",NULL,"12","Alfa Romeo","",""); # as much as you want!!! # --- Hope someone make use of it! I tried it with a SQL file with 32554 lines long! //elias "Lallous" <elias_bachaalany@yahoo.com> wrote in message news:20010814090205.32064.qmail@pb1.pair.com... > Actually the mysql_query() seems to accept only one statement at a time. > > Any work around so i can do query suchs: > > mysql_query(" > SELECT 1+1; > SHOW DATABASES; > USE database1; > SELECT * FROM table1; > "); > > Just multiple commands seperated with ';' ? (as if using the Mysql command > prompt) > >

« previous php.general (#62625) next »