PHP Classes

File: Example.Consolidated.php

Recommend this page to a friend!
  Classes of Dustin Ruckman   DB Access using PDO   Example.Consolidated.php   Download  
File: Example.Consolidated.php
Role: Example script
Content type: text/plain
Description: Example showing a few of the most commonly used functionality of the class
Class: DB Access using PDO
Query MySQL tables with prepared queries using PDO
Author: By
Last change: There was a naming issue with one of the variables that prevented the script from running.
Date: 9 years ago
Size: 2,946 bytes
 

Contents

Class file image Download
<?php

if (!isset($_POST['Action']))
   
ShowFormAndDie();

// Require the class and create an instance
require('path/to/DatabaseAccess.Class.php');
$DbAccess = new DatabaseAccess("yourHost", "dbName", "dbUser", "dbUserPassword");



// Create a new user from the data supplied by a form
// Create a blank row of data to create an ID
$numberOfRowsUpdated = $DbAccess->SimpleNon("INSERT INTO `UserAccounts` (`FirstName`) VALUES ('')");
// Clean up anything in the $_POST that is not account data.
unset($_POST['Action']);
// Now add the new ID to the _POST array
$_POST['ID'] = $DbAccess->LastInsertID();
// Use the Post array to update the database
$numberOfRowsUpdated = $DbAccess->UpdateTableViaArrayByID("UserAccounts", "ID", $_POST);



// Select a user from the Database
$DbAccess->PrepareStatement("SELECT * FROM `UserAccounts` WHERE `ID` = :ID");
$DbAccess->BindParameter(":ID", $ID);
$userData = $DbAccess->ExecuteQuery_Get();
// Note that data is always returned as an array of arrays
var_dump($userData[0]);



// Select multiple rows from the Database
$DbAccess->PrepareStatement("SELECT * FROM `UserAccounts`");
$userDatas = $DbAccess->ExecuteQuery_Get();
foreach (
$userDatas as $userData)
   
var_dump($userData);



// Loop the results ($userDatas) change the FirstName with ucfirst and update
// Create some placeholders for the data
$ID = "";
$newFirstName= "";

// Here we prepare the query that we will reuse
$DbAccess->PrepareStatement("UPDATE `UserAccounts` SET `FirstName` = :FirstName WHERE `ID` = :ID");
// Now bind the parameters that will later hold the data to use in the query
$DbAccess->BindParameter(":ID", $ID);
$DbAccess->BindParameter(":FirstName", $newFirstName);

// We want to use the key to access the array values
foreach (array_keys($userDatas) as $key)
{
   
// Update the placeholders
   
$ID = $userDatas[$key]['ID'];
   
$newFirstName= ucfirst($userDatas[$key]['FirstName']);
   
// Run the query, the parameters are automaticly updated with the placeholders
   
$numberOfRowsUpdated = $GLOBALS['DatabaseAccess']->ExecuteQuery_Non();
}



// Used to show a form for creating an UserAccount
function ShowFormAndDie() {
   
// Note that the name of each input is the same as the Table Column names
   
?>

<form method="post" action="./">
    <input type="hidden" name="Action" value="CreateUser" />
    <label for="FirstName">First Name</label>
        <input type="text" id="FirstName" name="FirstName" value="" />
    <label for="LastName" class="col-sm-2 control-label">Last Name</label>
        <input type="text" id="LastName" name="LastName" value="" />
    <label for="EMail">Email</label>
        <input type="email" id="EMail" name="EMail" value="" />
    <label for="ZipCode">ZipCode</label>
        <input type="text" id="ZipCode" name="ZipCode" value="" />
    <input type="submit">
</form>

    <?php
   
die();
}