DaoOne 。这是 Mysqli 的简单包装
这个库尽可能快。大多数操作都是简单的字符串/数组管理。
注意:此版本已移至 https://github.com/EFTEC/PdoOne
PdoOne 执行相同的工作,但它与 PDO 库(而不是 MySQLi)一起使用。目前,PdoOne 可与 Mysqli 和 SqlSrv 配合使用,但它还有许多其他DaoOne上不具备的功能
通过作曲家安装
作曲家需要 eftec/pdoone
该库可以与 eftec/ DaoOne协同工作。
更改类,而不是使用 eftec/ DaoOne -> eftec/pdoone
例子:
前:
/** @var eftec DaoOne $db */
$ db = null ;后:
/** @var eftecPdoOne $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' 如果我们使用DaoOne ::runGen(false),那么我们必须检查结果。 runGen ( DaoOne ) 返回一个 mysqli_result 对象。 runGen (PdoOne) 返回一个 pdostatement 对象。
前:
$ 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 ;转这个
$stmt->bind_param("s", $_POST['name']);
$stmt->execute();
$result = $stmt->get_result();
if($result->num_rows === 0) exit('No rows');
while($row = $result->fetch_assoc()) {
$ids[] = $row['id'];
$names[] = $row['name'];
$ages[] = $row['age'];
}
var_export($ages);
$stmt->close();
进入这个
$products=$dao
->select("*")
->from("myTable")
->where("name = ?",[$_POST['name']])
->toList();
将下一个要求添加到composer.json,然后更新composer。
{
"require" : {
"eftec/ DaoOne " : " ^3.15 "
}
}或者通过 cli 安装它
作曲家需要 eftec/ DaoOne
只需下载文件 lib/ DaoOne .php 并将其保存在文件夹中。
$ 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 );注意:您还可以使用过程链 insert($table,$schema,[$values]) 进行插入
$ dao -> runRawQuery ( ' insert into `product` (name) values(?) '
, array ( ' s ' , ' cocacola ' ));它返回一个 mysqli_statement。
$ 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.
}它开始一个事务
它提交一个事务。
它回滚事务。
如果为 true(默认),则如果发生错误,它将抛出错误。如果为 false,则继续执行
如果数据库已连接则为 true,否则为 false。
您还可以构建一个过程查询。
例子:
$ results = $ dao -> select ( " * " )-> from ( " producttype " )
-> where ( ' name=? ' , [ ' s ' , ' Cocacola ' ])
-> where ( ' idproducttype=? ' , [ ' i ' , 1 ])
-> toList (); 生成选择命令。
$ results = $ dao -> select ( " col1,col2 " )->. . .生成查询: select col1,col2 ....
$ results = $ dao -> select ( " select * from table " )->. . .生成查询: select * from table ....
生成选择命令。
$ results = $ dao -> select ( " col1,col2 " )-> distinct (). . .生成查询:选择不同的col1,col2 ....
注意:->distinct('unique') 返回 select unique ..
生成一个 from 命令。
$ results = $ dao -> select ( " * " )-> from ( ' table ' ). . .生成查询:select * from table
$tables可以是单个表或 SQL 结构。例如,下一个命令是有效的:
$ results = $ dao -> select ( " * " )-> from ( ' table t1 inner join t2 on t1.c1=t2.c2 ' ). . .生成一个 where 命令。
$ results = $ dao -> select ( " * " )
-> from ( ' table ' )
-> where ( ' p1=1 ' ). . .生成查询: select * from table where p1=1
注意:ArrayParameters 是一个数组,如下所示: type,value。
其中类型为 i=integer、d=double、s=string 或 b=blob。如有疑问,请使用“s”
数组参数示例:
['i',1,'s','你好','d',20.3,'s','世界']
$ results = $ dao -> select ( " * " )
-> from ( ' table ' )
-> where ( ' p1=? ' ,[ ' i ' , 1 ]). . .生成查询: select * from table where p1=?(1)
$ results = $ dao -> select ( " * " )
-> from ( ' table ' )
-> where ( ' p1=? and p2=? ' ,[ ' i ' , 1 , ' s ' , ' hello ' ]). . .生成查询: select * from table where p1=?(1) and p2=?('hello')
笔记。哪里可以嵌套。
$ results = $ dao -> select ( " * " )
-> from ( ' table ' )
-> where ( ' p1=? ' ,[ ' i ' , 1 ])
-> where ( ' p2=? ' ,[ ' s ' , ' hello ' ]). . .生成查询: select * from table where p1=?(1) and p2=?('hello')
您还可以使用:
$ results = $ dao -> select ( " * " )-> from ( " table " )
-> where ([ ' p1 ' => ' Coca-Cola ' , ' p2 ' => 1 ])
-> toList ();生成查询: select * from table where p1=?(Coca-Cola) and p2=?(1)
生成订单命令。
$ results = $ dao -> select ( " * " )
-> from ( ' table ' )
-> order ( ' p1 desc ' ). . .生成查询: select * from table order by p1 desc
生成组命令。
$ results = $ dao -> select ( " * " )
-> from ( ' table ' )
-> group ( ' p1 ' ). . .生成查询: select * from table group by p1
生成组命令。
$ results = $ dao -> select ( " * " )
-> from ( ' table ' )
-> group ( ' p1 ' )
-> having ( ' p1>? ' , array ( ' i ' , 1 )). . .生成查询: select * from table group by p1having p1>?(1)
注意:having 可以嵌套having()->having()
注意:having 可以不带参数having('col>10')
运行查询生成。
请注意,如果 returnArray 为 true,则它返回一个关联数组。如果 returnArray 为 false,则返回 mysqli_result
注意:它会重置当前参数(如当前 select、from、where 等)
这是runGen的宏。它返回一个关联数组或 null。
$ results = $ dao -> select ( " * " )
-> from ( ' table ' )
-> toList ()这是runGen的宏。它返回 mysqli_result 或 null。
$ results = $ dao -> select ( " * " )
-> from ( ' table ' )
-> toResult ()这是runGen的宏。它返回第一行(如果有,如果没有,则返回 false)作为关联数组。
$ results = $ dao -> select ( " * " )
-> from ( ' table ' )
-> first ()这是runGen的宏。它返回最后一行(如果有的话,如果没有,则返回 false)作为关联数组。
$ results = $ dao -> select ( " * " )
-> from ( ' table ' )
-> last ()有时运行 order() 和 first() 更有效,因为 last() 读取所有值。
它返回 sql 命令。
$ sql = $ dao -> select ( " * " )
-> from ( ' table ' )
-> sqlGen ();
echo $ sql ; // returns select * from table
$ results = $ dao -> toList (); // executes the query注意:它不会重置查询。
每个命令有四种执行方式。
假设我们要在col1列中添加一个值为20 的整数
使用值列表的架构和值:其中第一个值是列,第二个值是值的类型 (i=integer,d=double,s=string,b=blob),第二个数组包含值。
$ dao -> insert ( " table "
,[ ' col1 ' , ' i ' ]
,[ 20 ]);同一列表中的架构和值:其中第一个值是列,第二个是值的类型(i=integer,d=double,s=string,b=blob),第三个是值。
$ 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 ();或者(类型可能由 MySql 自动定义)
$ 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 ();生成查询: insert intoproducttype(idproducttype,name,type)values(?,?,?) ....
生成插入命令。
$ 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生成查询: update Producttype set
name=?,type=?其中idproducttype=? ....
生成删除命令。
$ dao -> delete ( " producttype "
,[ ' idproducttype ' , ' i ' ] // where
,[ 7 ]); // where $ dao -> delete ( " producttype "
,[ ' idproducttype ' => 7 ]); // where生成查询: delete from Producttype where
idproducttype=? ....
您还可以通过 DQL 构建器链进行删除。
$ dao -> from ( " producttype " )
-> where ( ' idproducttype=? ' ,[ ' i ' , 7 ]) // where
-> delete (); $ dao -> from ( " producttype " )
-> where ([ ' idproducttype ' => 7 ]) // where
-> delete (); 生成查询: delete from Producttype where
idproducttype=? ....
序列是 AUTO_NUMERIC 字段的替代项。它使用一个表来生成唯一的 ID。
使用的序列基于 Twitter 的 Snowflake,它是根据时间(微秒)、节点 ID 和序列生成的。这会生成一个唯一的 LONG (int 64) 值
$dao->nodeId=1; // optional
$dao->tableSequence='snowflake'; // optional
$dao->createSequence(); // it creates a table called snowflake and a function called next_snowflake()
$dao->getSequence() // string(19) "3639032938181434317"
$dao->getSequence(true) // returns a sequence by flipping some values.
$dao->getSequencePHP([unpredictable=false])不使用表返回序列。该序列比 $dao->getSequence 更有效,但它使用随机值来处理冲突。
如果 uppredictable 为真,那么它返回一个不可预测的数字(它会翻转一些数字)
$dao->getSequencePHP() // string(19) "3639032938181434317"
$dao->getSequencePHP(true) // string(19) "1739032938181434311"