PHP Classes

File: demo2-simpledboperations.php

Recommend this page to a friend!
  Classes of Bob Gombocki   PersistClass   demo2-simpledboperations.php   Download  
File: demo2-simpledboperations.php
Role: Example script
Content type: text/plain
Description: Executing DB queries
Class: PersistClass
DB access wrapper & storing objects in DB tables
Author: By
Last change: added web tutorial info
Date: 13 years ago
Size: 1,790 bytes
 

Contents

Class file image Download
<?php

/*
    http://coolpenguin.net/persistclass
    for updates, documentation, tutorials
*/


// connect to database (executing demo 1)
require('demo1-connection.php');

// get connection object
$con = DbConnectionPool::instance()->getConnection();

// execute an insert
$sql = 'INSERT INTO TESTTABLE(testcolumn) VALUES("INSERTSUCCESS")';
$con->query($sql);

// execute an update, retrieving number of updated rows
$sql = 'UPDATE TESTTABLE SET testcolumn = "UPDATESUCCESS"';
$modifiedRows = $con->query($sql);
// or $con->getRowsNum()

// perform select, iterating result
$sql = 'SELECT testcolumn FROM TESTTABLE';
$con->query($sql);
while(
$row = $con->next()) {
   
$testColumn = $row['testcolumn'];
}

// putting the result rows into an array
$sql = 'SELECT testcolumn FROM TESTTABLE';
$rows = $con->queryResultArr($sql);

// selecting a single row
$sql = 'SELECT testcolumn FROM TESTTABLE';
$row = $con->queryFirstRow($sql);

// selecting the first column of the first row
$sql = 'SELECT count(*) FROM TESTTABLE';
$count = $con->queryFirstRowCol($sql);

// selecting an array containing all rows first columns
$sql = 'SELECT TESTID from TESTTABLE';
$ids = $con->queryFirstColumnSet($sql);

// retrieving the total number of rows returned
$sql = 'SELECT testcolumn FROM TESTTABLE';
$con->query($sql);
$resultCount = $con->getRowsNum();

// retrieving ID of last inserted row
$sql = 'INSERT INTO TESTTABLE(testcolumn) VALUES("INSERTSUCCESS")';
$con->query($sql);
$lastId = $con->getLastId();

// escaping database input
$dangerousData = '"; DROP TABLES';
$sql = 'INSERT INTO TESTTABLE(testcolumn) VALUES('. $con->escape($dangerousData) .')';

// retrieving database link
$dbLink = $con->getLink();

echo
'Test successful';

?>