header('Content-type: text/html;charset=utf-8');//申明格式
require_once '../vendor/NotORM.php';
$pdo = new PDO("mysql:dbname=dbname","root","root");
$pdo->exec('set names utf8');//不加会乱码,申明格式
$structure = new NotORM_Structure_Convention(
$primary = "", // id_$table
$foreign = "", // id_$table
$table = "", // {$table}s
$prefix = "tp_" // tp_$table//表前缀
);
$db = new NotORM($pdo,$structure);
$db = new NotORM($pdo[, $structure[, $cache]]) |
Get database representation |
$db->debug = true |
Call error_log("$file:$line:$query; -- $parameters") before executing a query |
$db->debug = $callback |
Call $callback($query, $parameters) before executing a query, if $callback would return false then do not execute the query |
$db->debugTimer = $callback |
Call $callback() after executing a query |
$db->rowClass = 'ClassName' |
Derive row objects from this class (defaults to 'NotORM_Row' ) |
$db->transaction = $command |
Assign 'BEGIN' , 'COMMIT' or 'ROLLBACK' to start or stop transaction |
$table = $db->$tableName([$where[, $parameters[, ...]]]) |
Get representation of table $tableName ,查询$table所有信息 |
$table->where($where[, $parameters[, ...]]) |
Set WHERE (explained later) where |
$table->and($where[, $parameters[, ...]]) |
Add AND condition,对where的补充,and并且 |
$table->or($where[, $parameters[, ...]]) |
Add OR condition OR或 |
$table->order($columns[, ...]) |
Set ORDER BY , can be expression ("column DESC, id DESC" ) 排序 |
$table->order("") |
Reset previously set order 重置先前的排序 |
$table->select($columns[, ...]) |
Set retrieved columns, can be expression ("col, MD5(col) AS hash" ) |
$table->select("") |
Reset previously set select columns 重置之前设置选择列,选择字段,同TP的field |
$table->limit($limit[, $offset]) |
Set LIMIT and OFFSET,[num,offset],分别对应条数和开始下标 |
$table->group($columns[, $having]) |
Set GROUP BY and HAVING |
$table->union($table2[, $all]) |
Create UNION |
$table->lock($exclusive) |
Append FOR UPDATE (default) or LOCK IN SHARE MODE (non-exclusive) |
$array = $table->fetchPairs($key, $value) |
Fetch all values to associative array |
$array = $table->fetchPairs($key) |
Fetch all rows to associative array |
count($table) |
Get number of rows in result set 计算结果数量 |
(string) $table |
Get SQL query 获取SQL执行 |
$table($where[, $parameters[, ...]]) |
Shortcut for $table->where() since PHP 5.3 |
1.$table = $db->tp_img(); //SELECT * FROM tp_img
2.$db->tp_img()->fetch('title');//SELECT title FROM tp_img
$where = array('field1'=>111,'field2'=>222);其他条件,拆开单用,合起来用道理相同,这里复合起来统一说明
3.$tp_img = $db->tp_img()
->select('id,title,sort,info') //设置获取字段,相当于TP ->field
->where($where)//关联数组,与TP用法相同
->where("field3", $db->tp_img()) //二次查询,field IN (SELECT $primary FROM $tableName)"//默认返回的是查询结果的ID
->and('field4 like ?','%aa')//where条件的扩展,注意like书写格式
->order('sort ASC,id ASC')//order,排序
->limit(10,1);//[num,offset],分别对应条数和开始下标,同TP
$sql = "SELECT id,title,sort,info FROM tp_img WHERE (field1 = 111) AND (field2 = 222) AND (field IN ('9', '10')) AND (field3 like ?) ORDER BY sort ASC,id ASC LIMIT 10 OFFSET 1";
foreach ($table as $id => $row) |
Iterate all rows in result,迭代中所有行的结果 |
$row = $db->{$tableName}[$id] |
Get single row with ID $id from table $tableName (null if the row does not exist)通过ID获取表table中单行的值 |
$row = $table[$id] |
Get row with primary key $id from the result,从结果中获取主键值 |
$row = $table[array($column => $value)] |
Get first row with $column equals to $value |
$row = $table->fetch() |
Get next row from the result |
$data = $row[$column] |
Get data from column $column 从元素中获得值 |
$row2 = $row->$tableName |
Get row from referenced table 从引用表行 |
$table2 = $row->$tableName([$where[, $parameters[, ...]]]) |
Get result from referencing table 从引用表得到结果 |
$table2 = $row->$tableName()->via($column) |
Use non-default column (e.g. created_by ) |
foreach ($row as $column => $data) |
Iterate all values in row 遍历所有值的行 |
count($row) |
Get number of columns in row 得到的列数行 |
(string) $row |
Get primary key value 获取主键值 |
4.使用,对照上表,$table为上边查询出来的返回结果,不能直接用,得转换【括起来的为常用的】
foreach ($table as $id => $row){
$db->tp_img[$id]; //从tp_img表中获得单行的主键ID值
var_dump( $table[array('column' =>'value')] ); //【!!!获得第一行column = value的值】
echo $table[$id]; //返回单行结果的主键ID
echo $row['column']; //【!!!可以直接输出要查询的数据,column为字段名】
$re = $row->tp_img(); //object(NotORM_Row)
$re = $row->tp_img('id'); //object(NotORM_MultiResult)
$re = $row->tp_img()->via('classname'); //object(NotORM_MultiResult)
foreach ($row as $column => $data){} //【!!!每行的column与value值】
echo count($row); //【结果集$row中的个数】
echo (string) $row; //返回单行结果的主键ID
var_dump($table); //object(NotORM_Result)
var_dump($id); //【!!!返回单行结果的主键ID】
var_dump($row); //object(NotORM_Row)
}
foreach ($row as $column => $data){
$res[$id][$column] = $data;
print_r($column); //表中的字段名称column,eg:id |name |age,(注意一次循环只一个值)
print_r($data); //对应【该字段】的value值,eg:01 | 张三 | 10 (注意一次循环只一个值)
}
/**
* 方法一。转换数据结构,从对象类型转换成二维数组
* @param $obj
* @return array 二维数组
*/
function get_array($obj){
$arr = array();
foreach ($obj as $objSingle) {
$arrRow = array();
foreach ($objSingle as $key => $value) {
$arrRow[$key] = $value;
}
$arr[] = $arrRow;
}
return $arr;
}
/**
* 方法二。迭代器处理,转换数据结构,从对象类型转换成二维数组,
* @param $obj
* @return array 二维数组
*/
function iterator_array($obj) {
foreach($obj as $row) { $ret[] = iterator_to_array($row); }
return $ret;
}
$row = $db->$tableName()->insert($array) |
Insert a row into table $tableName and return the inserted row //返回 |
$row = $db->$tableName()->insert($array, ...) |
Insert multiple rows by a single INSERT and return the first row//用一条insert插入多条数据返回第一条的数据 |
$row = $db->$tableName()->insert_multi(array($array, ...)) |
Insert multiple rows by a single INSERT and return the number of inserted rows用一条insert插入多条数据返回第一条的数据,比第二条简介 |
$affected = $db->$tableName()->insert($result) |
Insert all rows of the result into $tableName using INSERT.. SELECT//插入查出来的info |
$affected = $table->update($array) |
Update all rows in table result |
$affected = $table->insert_update($unique, $insert, $update) |
Insert row or update if it already exists,插入或更新操作,就看$unipue是否存在了 |
$affected = $table->delete() |
Delete all rows in table result |
$affected = $row->update($array) |
Update single row |
$affected = $row->update() |
Update single row by real modifications ($row[$column] = $value or $row->$tableName = $row2 ) |
$affected = $row->delete() |
Delete single row |
$insert_id = $db->$tableName()->insert_id() |
Get last insert ID |
$db->freeze = true |
Disable persistence |
$insert_arr = array('uid'=>13,'text'=>'test','classid'=>111);//一条数据
$insert_arr2 = array('uid'=>13,'text'=>'test222','classid'=>222);//一条数据
$insert_arrs = array($insert_arr,$insert_arr,$insert_arr,$insert_arr,$insert_arr);//5条数据
$re = $db->img()->insert($insert_arr);//返回 object(NotORM_Row)
echo $insert_id = $db->img()->insert_id();//最后一个插入的ID
$re = $db->img()->insert($insert_arr,$insert_arr,$insert_arr,$insert_arr,$insert_arr);//返回插入的条数:5
echo $insert_id = $db->img()->insert_id();//返回插入5条中第一条的主键ID
echo $re = $db->img()->insert_multi($insert_arrs);//返回插入的条数:5
echo $insert_id = $db->img()->insert_id();//返回插入5条中第一条的主键ID
$db->img()->insert_update(array('id'=>18),$insert_arr,$insert_arr);//若id=18这条数据不存在,插入$insert_arr,存在,更新$insert_arr2,返回【插入条数|更新字段数】
4.更新:
$re = $db->img()->where('id',30);//返回 object(NotORM_Row)
$re->update($insert_arr2);//返回更新成功的条数
$db->img()->delete()//清空img表,返回删除的条数
$re= $db->img()->where('id',31);//查询出id=31的数据返回 object(NotORM_Row)
$re->delete();//删除id=31的数据,返回删除的条数,失败返回0