PHP code example of atiksoftware / php-class-db-pdo
1. Go to this page and download the library: Download atiksoftware/php-class-db-pdo library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
atiksoftware / php-class-db-pdo example snippets
use Atiksoftware\Database\PDOModel;
$pdomodel = new PDOModel();
$pdomodel->connect("localhost", "pdomodel_user", "J]GgvNr9UCUT", "pdomodel");
// Connect to mysql database
$pdomodel->connect("localhost", "pdomodel_user", "J]GgvNr9UCUT", "pdomodel");//connect to database
$pdomodel->connect("localhost", "root", "", "pdocrud" ,"mysql","utf8");//connect to database
// Connect to pgsql database
$pdomodel->connect("localhost", "root", "", "pdocrud","pgsql");//connect to database
$pdomodel->connect("localhost", "root", "", "pdocrud" ,"pgsql","utf8");//connect to database
// Connect to sqlite database
$pdomodel->connect("", "", "", "path-of-sqlite-file","sqlite");//connect to database
// another way
$pdomodel->dbSQLitePath = "path-of-sqlite-file";
$pdomodel->connect("", "", "", "" ,"sqlite","");//connect to database
//Example 1 - (query generated -> SELECT `empId`,`firstName`,`lastName` FROM `emp` )
//Specify column names
$pdomodel->columns = array("empId", "firstName", "lastName");
$result = $pdomodel->select("emp");
//Example 2 - (query generated -> SELECT * FROM `emp` WHERE `age`>= ? )
//Specify where condition
$pdomodel->where("age",30,">=");
$result = $pdomodel->select("emp");
//Example 3 - (query generated -> SELECT * FROM `emp` WHERE `status`= ? AND `age`>= ? AND ( `firstName`= ? OR `firstName`= ? ) )
// Example of use of multiple "and" and "or" with brackets
$pdomodel->where("status", 1);
$pdomodel->where("age",30,">=");
$pdomodel->openBrackets = "(";
$pdomodel->where("firstName", 'John');
$pdomodel->andOrOperator = "OR";
$pdomodel->where("firstName", 'bob');
$pdomodel->closedBrackets = ")";
$result = $pdomodel->select("emp");
//Example 4 - (query generated ->SELECT * FROM `emp` GROUP BY `age` ORDER BY `firstName` LIMIT 0,5 )
//groupby, order by and limit example
$pdomodel->groupByCols = array("age");
$pdomodel->orderByCols = array("firstName");
$pdomodel->limit = "0,5";
$result = $pdomodel->select("emp");
//Example 5 - (query generated ->SELECT * FROM `emp` WHERE `firstName`LIKE ? AND `age` BETWEEN ? AND ? AND `empId`IN (?,?) ORDER BY `firstName` LIMIT 0,5 )
// LIKE, BETWEEN, IN, NOT IN example
$pdomodel->andOrOperator = "AND";
$pdomodel->where("firstName", "Jo%", "LIKE");
$pdomodel->where("age", array(10,50), "BETWEEN");
$pdomodel->where("empId", array(30,50), "IN");
$pdomodel->limit = "0,5";
$result = $pdomodel->select("emp");
//Example 6 - (query generated ->SELECT count(*), concat(firstname, ' ' , lastname) AS `fullname` FROM `emp` ORDER BY `firstName` )
// Aggregate functions like count example
$pdomodel->columns = array("count(*), concat(firstName, ' ' , lastName) as fullName");
$result = $pdomodel->select("emp");
//Example 7 - (query generated ->SELECT * FROM `emp` GROUP BY `firstName` HAVING sum(age)>10 ORDER BY `firstName` )
$pdomodel->groupByCols = array("firstName");
$pdomodel->havingCondition = array("sum(age)>10");
$result = $pdomodel->select("emp");
//Example 8 - (query generated ->SELECT * FROM `wp_postmeta` WHERE `post_id` IN (select post_id from wp_posts where post_id>?) )
//use of subquery with where condition
$pdomodel->where_subquery("post_id", "select post_id from wp_posts where post_id>?", "IN", array(1));
$result = $pdomodel->select("wp_postmeta");
//Example 9 - (query generated ->SELECT * FROM `emp` ORDER BY `firstName` LIMIT 0,3 )
$pdomodel->fetchType = "OBJ";
$pdomodel->limit = "0,3";
$result = $pdomodel->select("emp");
$pdomodel->insert("order", array("orderNumber"=>1001, "customerName"=>"John Cena", "address"=>"140 B South Jercy");
//Example 1
$insertData = array("orderNumber"=>1001, "customerName"=>"John Cena", "address"=>"140 B South Jercy");
$pdomodel->insert("order", $insertData);
//Example 2
$insertEmpData["firstName"] = "Simon";
$insertEmpData["lastName"] = "jason";
$pdomodel->insert("emp", $insertEmpData);
/* Sub query function */
$pdomodel->subQuery("select post_id from wp_postmeta where meta_id=?","postmeta", array(20));
$result = $pdomodel->select("wp_posts");
//Example 1
//subquery with where and order by etc.
$pdomodel->subQuery("select post_id from wp_postmeta where meta_id=?","postmeta", array(20));
$pdomodel->where("p.id", 10, ">=");
$pdomodel->orderByCols = array("p.id");
$result = $pdomodel->select("wp_posts as p")
# CSV Export
/* array to csv function */
$data = array( array("row1col1","row1col2","row1col3","row1col4"),array("row2col1","row2col2","row2col3","row2col4"));
$pdomodel->arrayToCSV($data);
//Example 1
$records = $pdomodel->select("emp"); //get data from table
$pdomodel->arrayToCSV($records, "emp.csv");//export it to csv
# PDF Export
/* array to pdf function */
$data = array( array("row1col1","row1col2","row1col3","row1col4"),array("row2col1","row2col2","row2col3","row2col4"));
$pdomodel->arrayToPDF($data);
//Example 1
$records = $pdomodel->select("emp"); //get data from table
$pdomodel->arrayToPDF($records, "emp.pdf");//export it to pdf
# Excel Export
/* array to excel function */
$data = array( array("row1col1","row1col2","row1col3","row1col4"),array("row2col1","row2col2","row2col3","row2col4"));
$pdomodel->arrayToExcel($data);
//Example 1
$records = $pdomodel->select("emp"); //get data from table
$pdomodel->arrayToExcel($records, "emp.xlsx");//export it to excel
# HTML Export
/* array to html function */
$data = array( array("row1col1","row1col2","row1col3","row1col4"),array("row2col1","row2col2","row2col3","row2col4"));
$pdomodel->arrayToHTML($data);
echo $pdomodel->outputHTML; // echo output html
//Example 1
$records = $pdomodel->select("emp"); //get data from table
$pdomodel->arrayToHTML($records, "emp.html");//export it to html
echo $pdomodel->outputHTML; // read output html
# XML Export
/* array to xml function */
$data = array( array("row1col1","row1col2","row1col3","row1col4"),array("row2col1","row2col2","row2col3","row2col4"));
$pdomodel->arrayToXML($data);
//Example 1
$records = $pdomodel->select("emp"); //get data from table
$pdomodel->arrayToXML($records, "emp.xml");//export it to xml
# JSON Export
/* array to json function */
$data = array( array("row1col1","row1col2","row1col3","row1col4"),array("row2col1","row2col2","row2col3","row2col4"));
$pdomodel->arrayToJson($data);
//Example 1
$records = $pdomodel->select("emp"); //get data from table
$pdomodel->arrayToJson($records);//export it to json
# CSV Import
/* csv to array function */
$records = $pdomodel->csvToArray("emp.csv");
//Example 2
$records = $pdomodel->csvToArray("emp.csv");
$pdomodel->insertBatch("emp", $records);
# Excel Import
/* excel to array function */
$records = $pdomodel->excelToArray("emp.xls");
//Example 2
$records = $pdomodel->excelToArray("emp.xls");
$pdomodel->insertBatch("emp", $records);
# XML Import
/* xml to array function */
$pdomodel->isFile=true;
$records = $pdomodel->xmlToArray("emp.xml");
//Example 2
$pdomodel->isFile=true;
$records = $pdomodel->xmlToArray("emp.xml");
$pdomodel->insertBatch("emp", $records);