Posts

Showing posts with the label pdo

Querying a SQLite 3 database using PHP

Previously, I detailed a way of creating a database in A basic hit counter using PHP and SQLite 3 using PHP Data Objects and PHP 5. For most web sites, SQLite would be fine, but for very high volume (as in many hundreds of thousands of hits per day), there are better options - Appropriate Uses For SQLite (SQLite.org) has more details on when SQLite is a good option. Here is an example of how to query the data and display it on a page (to see which pages are popular for example). <? $dbfolder = $_SERVER["DOCUMENT_ROOT"]."/data/"; $dbname = $_SERVER["HTTP_HOST"]."_log.sq3"; $logdb = new PDO("sqlite:".$dbfolder.$dbname); $starttable = "<table> <tr> <th>Page</th> <th>Counter</th> </tr>"; $endtable = "</table>"; $tablecontents = ""; foreach ($logdb->query("SELECT * FROM hits ORDER BY counter DESC") as $row) { $tablecontents .= " <t...

A basic hit counter using PHP and SQLite 3

SQLite is a lightweight database engine that is bundled with PHP 5. Using PHP Data Objects (PDO) you can create and edit SQLite databases and tables. A SQLite databases is just a single file that is stored in a folder that has modify rights applied to it (otherwise you can't change it). This sample shows how to create a database, add a table to it and insert and update a record in it. By using PDO, you can easily change it to use different database engines (like MySQL, IBM DB2 etc [others, like MSSQL and Oracle are in a experimental stage though, check the PDO site (linked above) for more information]). You can copy this code into a separate file and include it within pages that you want a counter on &lt? include("counter.php"); ?> . <? // logging page hits $dbfolder = $_SERVER["DOCUMENT_ROOT"]."/data/"; $dbname = $_SERVER["HTTP_HOST"]."_log.sq3"; // check if database file exists first if(!file_exists($dbfolder.$dbna...