PHP Classes

File: Example.UpdateByReusingQuery.php

Recommend this page to a friend!
  Classes of Dustin Ruckman   DB Access using PDO   Example.UpdateByReusingQuery.php   Download  
File: Example.UpdateByReusingQuery.php
Role: Example script
Content type: text/plain
Description: An example using variable data with a static query to update multiple records
Class: DB Access using PDO
Query MySQL tables with prepared queries using PDO
Author: By
Last change:
Date: 9 years ago
Size: 980 bytes
 

Contents

Class file image Download
<?php

require('path/to/DatabaseAccess.Class.php');

$DbAccess = new DatabaseAccess("yourHost", "dbName", "dbUser", "dbUserPassword");

$userDatas = $DbAccess->SimpleGet("SELECT * FROM `UserAccounts`");

// Create some placeholders for the data
$ID = "";
$FirstName = "";

// 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'];
   
$FirstName = ucfirst($userDatas[$key]['FirstName']);
   
// Run the query, the parameters are automaticly updated with the placeholders
   
$numberOfRowsUpdated = $GLOBALS['DatabaseAccess']->ExecuteQuery_Non();
}