PHP code example of eftec / daoone

1. Go to this page and download the library: Download eftec/daoone 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/ */

    

eftec / daoone example snippets


/** @var \eftec\DaoOne $db */
$db=null;

/** @var \eftec\PdoOne $db */
$db=null;

$db=new DaoOne('127.0.0.1','root','abc.123','sakila');

$db=new DaoOne('mysql','127.0.0.1','root','abc.123','sakila'); // check 'mysql'

$result=$db->runGen(false); // it returns a mysqli_result
$result->fetch_assoc();
$result->free_result();

$result=$db->runGen(false); // it returns a pdostatement
$result->fetch( PDO::FETCH_ASSOC);
$result=null;

$dao=new DaoOne("127.0.0.1","root","abc.123","sakila","");
$dao->connect();

$sql="CREATE TABLE `product` (
    `idproduct` INT NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(45) NULL,
    PRIMARY KEY (`idproduct`));";
$dao->runRawQuery($sql);  

$sql="insert into `product`(name) values(?)";
$stmt=$dao->prepare($sql);
$productName="Cocacola";
$stmt->bind_param("s",$productName); // s stand for string. Also i =integer, d = double and b=blob
$dao->runQuery($stmt);

$dao->runRawQuery('insert into `product` (name) values(?)'
    ,array('s','cocacola'));

    $sql="select * from `product` order by name";
    $stmt=$dao->prepare($sql);
    $dao->runQuery($stmt);
    $rows = $stmt->get_result();
    while ($row = $rows->fetch_assoc()) {
        var_dump($row);
    }
    

    $sql="select * from `product` order by name";
    $stmt=$dao->prepare($sql);
    $dao->runQuery($stmt);
    $rows = $stmt->get_result();
    $allRows=$rows->fetch_all(MYSQLI_ASSOC);
    var_dump($allRows);

try {
    $sql="insert into `product`(name) values(?)";
    $dao->startTransaction();
    $stmt=$dao->prepare($sql);
    $productName="Fanta";
    $stmt->bind_param("s",$productName); 
    $dao->runQuery($stmt);
    $dao->commit(); // transaction ok
} catch (Exception $e) {
    $dao->rollback(false); // error, transaction cancelled.
}

$results = $dao->select("*")->from("producttype")
    ->where('name=?', ['s', 'Cocacola'])
    ->where('idproducttype=?', ['i', 1])
    ->toList();   

$results = $dao->select("col1,col2")->...

$results = $dao->select("select * from table")->...

$results = $dao->select("col1,col2")->distinct()...

$results = $dao->select("*")->from('table')...

$results = $dao->select("*")->from('table t1 inner join t2 on t1.c1=t2.c2')...

$results = $dao->select("*")
->from('table')
->where('p1=1')...

$results = $dao->select("*")
->from('table')
->where('p1=?',['i',1])...

$results = $dao->select("*")
->from('table')
->where('p1=? and p2=?',['i',1,'s','hello'])...

$results = $dao->select("*")
->from('table')
->where('p1=?',['i',1])
->where('p2=?',['s','hello'])...

$results = $dao->select("*")->from("table")
    ->where(['p1'=>'Coca-Cola','p2'=>1])
    ->toList();

$results = $dao->select("*")
->from('table')
->order('p1 desc')...

$results = $dao->select("*")
->from('table')
->group('p1')...

$results = $dao->select("*")
->from('table')
->group('p1')
->having('p1>?',array('i',1))...

$results = $dao->select("*")
->from('table')
->toList()

$results = $dao->select("*")
->from('table')
->toResult()

$results = $dao->select("*")
->from('table')
->first()

$results = $dao->select("*")
->from('table')
->last()

$sql = $dao->select("*")
->from('table')
->sqlGen();
echo $sql; // returns select * from table
$results=$dao->toList(); // executes the query

$dao->insert("table"
    ,['col1','i']
    ,[20]);

$dao->insert("table"
    ,['col1','i',20]);

$dao->insert("table"
    ,['col1'=>'i']
    ,['col1'=>20]);

$dao->insert("table"
    ,['col1'=>20]);

$dao->insert("producttype"
    ,['idproducttype','i','name','s','type','i']
    ,[1,'cocacola',1]);

    $dao->from("producttype")
        ->set(['idproducttype','i',0 ,'name','s','Pepsi' ,'type','i',1])
        ->insert();

    $dao->from("producttype")
        ->set("idproducttype=?",['i',101])
        ->set('name=?',['s','Pepsi'])
        ->set('type=?',['i',1])
        ->insert();

    $dao->from("producttype")
        ->set("idproducttype=?",['i',101])
        ->set('name=?','Pepsi')
        ->set('type=?',1)
        ->insert();

    $dao->from("producttype")
        ->set('(idproducttype,name,type) values (?,?,?)',['i',100,'s','Pepsi','i',1])
        ->insert();

$dao->update("producttype"
    ,['name','s','type','i'] //set
    ,[6,'Captain-Crunch',2] //set
    ,['idproducttype','i'] // where
    ,[6]); // where

$dao->update("producttype"
    ,['name'=>'Captain-Crunch','type'=>2] // set
    ,['idproducttype'=>6]); // where

$dao->from("producttype")
    ->set("name=?",['s','Captain-Crunch']) //set
    ->set("type=?",['i',6]) //set
    ->where('idproducttype=?',['i',6]) // where
    ->update(); // update

$dao->from("producttype")
    ->set("name=?",'Captain-Crunch') //set
    ->set("type=?",6) //set
    ->where('idproducttype=?',['i',6]) // where
    ->update(); // update

$dao->delete("producttype"
    ,['idproducttype','i'] // where
    ,[7]); // where

$dao->delete("producttype"
    ,['idproducttype'=>7]); // where

$dao->from("producttype")
    ->where('idproducttype=?',['i',7]) // where
    ->delete(); 

$dao->from("producttype")
    ->where(['idproducttype'=>7]) // where
    ->delete();