User Tools

Site Tools


snippets:php

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
snippets:php [2025-11-10 15:19] – Abschnitt „SQLite3“ hinzugefügt malte70snippets:php [2025-11-10 15:56] (current) – [SQLite3] Quellcode-Kommentare malte70
Line 42: Line 42:
   * [[https://www.php.net/manual/en/class.sqlite3.php|PHP: SQLite3 - Manual]]   * [[https://www.php.net/manual/en/class.sqlite3.php|PHP: SQLite3 - Manual]]
   * [[https://www.php.net/manual/en/class.sqlite3stmt.php|PHP: SQLite3Stmt - Manual]]   * [[https://www.php.net/manual/en/class.sqlite3stmt.php|PHP: SQLite3Stmt - Manual]]
 +  * [[https://www.php.net/manual/en/class.sqlite3result.php|PHP: SQLite3Result - Manual]]
  
 <file php sqlite3.php> <file php sqlite3.php>
 <?php <?php
 +// Open database and create it if necessary
 $db = new SQLite3("dbfile.sqlite3"); $db = new SQLite3("dbfile.sqlite3");
  
 +// Initialize the database with a table and some data
 $db->exec('CREATE TABLE users(id INT NOT NULL PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, email TEXT DEFAULT NULL, password TEXT NOT NULL'); $db->exec('CREATE TABLE users(id INT NOT NULL PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, email TEXT DEFAULT NULL, password TEXT NOT NULL');
 $db->exec('INSERT INTO users (name, email, password) VALUES ("john.doe", "john.doe@example.com", "1234"));'); $db->exec('INSERT INTO users (name, email, password) VALUES ("john.doe", "john.doe@example.com", "1234"));');
 $db->exec('INSERT INTO users (name, email, password) VALUES ("jane.doe", "jane.doe@example.com", "asdf"));'); $db->exec('INSERT INTO users (name, email, password) VALUES ("jane.doe", "jane.doe@example.com", "asdf"));');
  
 +// Create a SQLite3Stmt, bind some values and execute the SQL command safely
 $stmt = $db->prepare('SELECT * FROM users WHERE id=:id AND name=:name'); $stmt = $db->prepare('SELECT * FROM users WHERE id=:id AND name=:name');
 $stmt->bindValue(':id', 1, SQLITE3_INTEGER); $stmt->bindValue(':id', 1, SQLITE3_INTEGER);
 $stmt->bindValue(':name', "john.doe", SQLITE3_TEXT); $stmt->bindValue(':name', "john.doe", SQLITE3_TEXT);
  
 +// Execute statement and get first row as an associative Array
 $res = $stmt->execute(); $res = $stmt->execute();
 var_dump($res->fetchArray()); var_dump($res->fetchArray());
snippets/php.txt · Last modified: by malte70