上海城乡住房建设厅网站,asp网站怎么做301定向,钢筋网片150x150一个平方多少吨,网站建设佰首选金手指十四单数据查询
单条数据查询#xff0c;一般是一维数组 Db::table()中 table 必须指定完整数据表#xff08;包括前缀#xff09;#xff0c;如果配置了表前缀#xff0c;Db::name()中可以忽略 如果希望只查询一条数据#xff0c;可以使用 find()方法#xff0c;需指定 wh…单数据查询
单条数据查询一般是一维数组 Db::table()中 table 必须指定完整数据表包括前缀如果配置了表前缀Db::name()中可以忽略 如果希望只查询一条数据可以使用 find()方法需指定 where 条件
public function index()
{$user Db::table(tp_user)-where(id, 1)-find();//$user Db::name(user)-where(id, 1)-find();return json($user);
}Db::getLastSql()方法可以得到最近一条 SQL 查询的原生语句
public function index()
{$user Db::table(tp_user)-where(id, 1)-find();echo print_r($user-array());echo br;echo Db::getLastSql();
}没有查询到任何值则返回 null
public function index()
{$user Db::table(tp_user)-where(id, 110)-find();echo $user;
}使用 findOrFail()方法同样可以查询一条数据在没有数据时抛出一个异常
public function index()
{$user Db::table(tp_user)-where(id, 110)-findOrFail();echo $user;}使用 findOrEmpty()方法也可以查询一条数据但在没有数据时返回一个空数组
public function index()
{$user Db::table(tp_user)-where(id, 110)-findOrEmpty();return json($user);
}数据集查询
查询多条数据一般是二维数组 想要获取多列数据可以使用 select()方法
public function index()
{$users Db::table(tp_user)-select();echo Db::getLastSql();echo br;return json($users);
}多列数据在查询不到任何数据时返回空数组使用 selectOrFail()抛出异常
public function index()
{$users Db::table(tp_user)-where(id,110)-selectOrFail();return json($users);
}在 select()方法后再使用 toArray()方法可以将数据集对象转化为数组
public function index()
{$users Db::table(tp_user)-where(id,110)-select();dump($users);dump($users-toArray());
}其它查询
通过 value()方法可以查询指定字段的值单个没有数据返回 null
public function index()
{$username Db::table(tp_user)-where(id, 2)-value(username);echo $username;$username Db::table(tp_user)-where(id, 111)-value(username);echo $username;
}可以指定 id 作为列值的索引
public function index()
{$user Db::name(user)-column(username, id);return json($user);
}如果处理的数据量巨大成百上千那种一次性读取有可能会导致内存开销过大为了避免内存处理太多数据出错可以使用 chunk()方法分批处理数据比如每次只处理 100 条处理完毕后再读取 100 条继续处理 这里数据量不够100每次取五条
public function index()
{Db::table(tp_user)-chunk(5, function ($users) {foreach ($users as $user) {dump($user);}echo 1;});
}可以利用游标查询功能可以大幅度减少海量数据的内存开销它利用了 PHP 生成器特性。每次查询只读一行然后再读取时自动定位到下一行继续读取
public function index()
{$cursor Db::table(tp_user)-cursor();foreach ($cursor as $user) {dump($user);}
}