PHP Classes

File: db_example.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in Bookmark in
  Classes of Graham Trott  >  Simple MySQL wrapper  >  db_example.php  >  Download  
File: db_example.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Simple MySQL wrapper
MySQL access wrapper based on static functions
Author: By
Last change:
Date: 6 years ago
Size: 2,200 bytes


Class file image Download
    // An example of using the db_mysql functions.
    // Written by Graham Trott (
    // For unrestricted use.

    // This example is intended to run from the command line.
    // It builds a small database containing three records.
    // You should see the following console output:

    // Connected to database.
    // Table created.
    // Barack Obama is president of The United States.
    // Silvio Berlusconi is prime minister of Italy.
    // Gordon Broon is prime minister of The United Kingdom.
    // Gordon Brown is prime minister of The United Kingdom.

require_once "db_mysql.php";

// Connect to the database. Provide your own username and password.
DB::connect("localhost", "username", "password", "example");
"Connected to database.\n";

// Create a table. If it's already there, drop it.
if (DB::tableExists("mytable")) DB::dropTable("mytable");
DB::createTable("mytable", array(
"Table created.\n";

// Create a couple of records.
DB::insert("mytable", array(
"name"=>"Barack Obama",
"country"=>"The United States",
DB::insert("mytable", array(
"name"=>"Gordon Broon", // deliberate mistake
"country"=>"The United Kingdom",
"title"=>"prime minister",
DB::insert("mytable", array(
"name"=>"Silvio Berlusconi",
"title"=>"prime minister",

// Prove it worked.
$result = DB::select("mytable", "*", "ORDER BY prefix");
    while (
$row = DB::fetchRow($result))
$row->name . " is " . $row->title . " of " . $row->country . ".\n";

// Now fix the deliberate mistake.
DB::update("mytable", array(
"name"=>"Gordon Brown"
), "WHERE prefix=44");

// Now prove it again. This time, just select the fields we want.
$row = DB::selectRow("mytable", array("name", "title", "country"), "WHERE prefix=44");
$row->name . " is " . $row->title . " of " . $row->country . ".\n";