note 42027 added to function.sqlite-query

From: Date: Thu, 29 Apr 2004 22:41:59 +0000
Subject: note 42027 added to function.sqlite-query
Groups: php.notes 
Request: Send a blank email to php-notes+get-69016@lists.php.net to get a copy of this message
The function below allows you to submit multiple queries in one shot to a SQLITE database, and will return whatever you would get for the final query. function sqlite_query_multi ($db, $query) { // submit multiple queries (separated by ;) to $db // and return the result from the last one $multiSQL = "/('[^']*'|\"[^\"]*\"|[^;'\"])*;/"; preg_match_all ($multiSQL, "$query;", $aSQL); for ($i=sizeof($aSQL=$aSQL[0]);$i--;) if (!($trim=trim(substr($aSQL[$i],0,-1)))) unset ($aSQL[$i]); else $aSQL[$i] = "$trim;"; foreach ($aSQL as $i => $sql) $dbRes = sqlite_query ($db, $sql); return (@$dbRes); } The section below illustrates the above function: $db = sqlite_open(":memory:", 0666, $sqliteerror); $query = <<<EOD CREATE TABLE foo (bar INTEGER PRIMARY KEY, baz TEXT); INSERT INTO foo VALUES (Null, 'Hi'); INSERT INTO foo VALUES (Null, 'Mom'); SELECT * FROM foo; EOD; $dbRes = sqlite_query_multi ($db, $query); // 4 statements while (sqlite_has_more($dbRes)) var_dump(sqlite_fetch_array($dbRes, SQLITE_ASSOC)); Csaba Gabor ---- Manual Page -- http://www.php.net/manual/en/function.sqlite-query.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+42027 Delete -- http://master.php.net/manage/user-notes.php?action=delete+42027&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+42027&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#69016) next »