PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

sqlite_rewind> <sqlite_prev
Last updated: Fri, 05 Sep 2008

view this page in

sqlite_query

(PHP 5, PECL sqlite:1.0-1.0.3)

sqlite_queryFührt auf einer Datenbank eine Abfrage durch und liefert das Abfrageergebnis zurück.

Beschreibung

resource sqlite_query ( resource $db , string $Abfrage )
resource sqlite_query ( string $Abfrage , resource $db )

Führt die den durch Abfrage gegebenen SQL-Befehl auf der Datenbank db durch,

Für Abfrage, die Zeilen zurückliefern, gibt diese Funktion ein Abfrageergebnis zurück, dass von Funkionen wie sqlite_fetch_array() oder auch sqlite_seek() genutzt werden kann.

Für alle anderen Arten von Abfragen gibt diese Funktion ein boolsches Resultat zurück, TRUE bei Erfolg und FALSE bei einem Fehler.

Unabhänging vom Abfragetyp liefert die Funktion FALSE zurück wenn die Abfrage fehlschlägt.

sqlite_query() gib ein gepuffertes Ergebnis, dessen Zeilen einzeln Ansprechbar sind. Dies ist für kleine Abfragen nützlich, bei denen man abwechselnd unterschiedliche Zeilen abfragen will. Die gepufferten Ergebnisse belegen Speicher, um das gesamte Ergebnis halten zu können. Dieser Speicher wird erst dann wieder freigegeben, wenn das komplette Ergebnis durchlaufen wurde. Wenn nur ein sequentieller Zugriff auf die Daten nötig ist, wird empfohlen, die performantere Funktion sqlite_unbuffered_query() zu nutzen.

Hinweis: Two alternative syntaxes are supported for compatibility with other database extensions (such as MySQL). The preferred form is the first one, where the db parameter is the first parameter to the function.

Warnung

SQLite will execute multiple queries separated by semicolons, so you can use it to execute a batch of SQL that you have loaded from a file or have embedded in a script.

When executing multiple queries, the return value of this function will be FALSE if the was an error, but undefined otherwise (it might be TRUE for success or it might return a result handle).

See also sqlite_array_query().



sqlite_rewind> <sqlite_prev
Last updated: Fri, 05 Sep 2008
 
add a note add a note User Contributed Notes
sqlite_query
helmakil at gmail dot com
05-Feb-2008 10:58
I suppose this could be useful for users attempting to use a sqlite database for the first time.
<?php
$database
= new SQLiteDatabase($yourfile, 0666, $error);
if (!
$database) {
   
$error = (file_exists($yourfile)) ? "Impossible to open, check permissions" : "Impossible to create, check permissions";
    die(
$error);
}
$query = $database->query("SELECT name FROM sqlite_master WHERE type='table'", SQLITE_ASSOC, $query_error); #Lists all tables
if ($query_error)
    die(
"Error: $query_error"); #This means that most probably we catch a syntax error
if (!$query)
    die(
"Impossible to execute query.") #As reported above, this means that the db owner is different from the web server's one, but we did not commit any syntax mistake.
print $query->numRows();
while (
$row = $query->fetch())
    print(
$row['name']."\n");
?>
i suppose that the example above is also useful because it will list all the tables created, giving also comprehension of what appens when managing a sqlite database in OO mode.
jason at fatpipeinc dot com
27-Oct-2005 04:32
Correction:

sqlite_query will return NULL if the web server cannot write to the sqlite database file.

(please correct the previous post and delete this one)
jason at fatpipeinc dot com
24-Oct-2005 06:35
sqlite_open will return NULL if the web server cannot write to the sqlite database file.

I saw the following message in my web server error log:

PHP Warning:  sqlite_query(): (null) ...

It turns out that the sqlite database file was owned by a user other than the one the web server was running as.  In my case, it was a Linux system running Apache (which was running under the context of user apache).  The sqlite database file was owned by root.  I changed ownership of the file to user apache and now it works!  The sqlite_open call now returns a valid result handle.

Jason Aeschilman
05-Oct-2004 10:54
While reading the manual at sqlite.org, I can answer for the quotes in strings. You should put two quote to get one.

insert into atable values ( '5 O''Clock');
csaba at alum dot mit dot edu
30-Apr-2004 03:41
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

sqlite_rewind> <sqlite_prev
Last updated: Fri, 05 Sep 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites