PHP Classes

File: example/insert_demo.php

Recommend this page to a friend!
  Classes of Slawomir Kaleta   Dframe Database   example/insert_demo.php   Download  
File: example/insert_demo.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Dframe Database
Access a MySQL database using PDO
Author: By
Last change: Update of example/insert_demo.php
Date: 3 years ago
Size: 2,060 bytes
 

Contents

Class file image Download
<?php

//300 seconds = 5 minutes execution time
ini_set('max_execution_time', 300);
// overrides the default PHP memory limit.
ini_set('memory_limit', '-1');

// include pdo helper class to use common methods
require_once '../src/Helper/PDOHelper.php';
// include pdo class wrapper
require_once '../src/class.pdowrapper.php';

// database connection setings
$dbConfig = ['host' => 'localhost', 'dbname' => 'sampledb', 'username' => 'root', 'password' => ''];
// get instance of PDO Wrapper object
$db = new PdoWrapper($dbConfig);

// get instance of PDO Helper object
$helper = new PDOHelper();

// set error log mode true to show error on screen or false to log in log file
$db->setErrorLog(true);

// Example -1
$dataArray = ['first_name' => 'Sid', 'last_name' => 'Mike', 'age' => 45];
// use insert function
$q = $db->insert('test', $dataArray)->showQuery()->getLastInsertId();
PDOHelper::PA($q);

// Example -2
$dataArray = ['first_name' => 'Scott', 'last_name' => 'Dimon', 'age' => 55];
// use insert function
$q = $db->insert('test', $dataArray)->showQuery()->getLastInsertId();
PDOHelper::PA($q);

// Example -3
$dataArray = ['first_name' => 'Simran', 'last_name' => 'Singh', 'age' => 25];
// use insert function
$q = $db->insert('testt', $dataArray)->showQuery()->getLastInsertId();
PDOHelper::PA($q);

// Example -4
// use insert function
$q = $db->insert('test', $dataArray)->showQuery()->getLastInsertId();
// print array last insert id
PDOHelper::PA($q);

// Example -5 (Bulk Insert)
// loop start to create insert data
$dataArray = [];
for (
$i = 0; $i < 1000000; $i++) {
   
$dataArray[] = ['first_name' => 'Sid', 'last_name' => 'Mike', 'age' => 45];
   
$dataArray[] = ['first_name' => 'Scott', 'last_name' => 'Dimon', 'age' => 78];
   
$dataArray[] = ['first_name' => 'Meena', 'last_name' => 'Verma', 'age' => 23];
}
// use insertBatch function to insert multiple row at once and get all last insert id in array
$q = $db->insertBatch('test', $dataArray, true)->showQuery()->getAllLastInsertId();
// print array last insert id
PDOHelper::PA($q);