thinkphp5.1查询数据
web ThinkPHP5.1 2019-01-07 2427 0
关于本站

“最难不过坚持”

本人承接扒站仿站,php网站维护,病毒查杀,网站编辑,网站改版,html制作

有需要网站维护,改版,病毒查杀,网站编辑,网站备案,html制作等相关的工作可以联系我。
本人有多年相关工作经验,也可提供免费咨询,交个朋友。
有需要探讨问题的朋友,也可以加我微信,共同探讨!
微信:15011482830 QQ:408917339

6405032 2611 39
最新评论
https://jueru.net/
评 留言
:weixiao:
评 留言
:shuijiao: :weiqu: :zhenbang: :leng:
评 留言
:yiwen: :yiwen: :yiwen: :yiwen:
评 EasySass: could not generate CSS file. See Output panel for details.
这个业务逻辑多少都有点奇怪了,阅读浏览次数增值在新闻详情页的控制器方法里setInc,这怎么还写进模型事件里了。如果非要用onAfterRead也可以,把新闻文章的内容单独分出来一个news_content表,然后把它和news做关联,然后给news_content表的onAfterRead事件做增值处理,这样点进新闻页内查询到文章内容时才会触发它。
评 TP6模型事件-查询后onAfterRead不好用
文章标签更多
ThinkPHP (254)
Mysql (58)
DedeCms (33)
jQuery (67)
证件照 (1)
setInc (4)
setDec (4)
onclick (5)
<?php
namespace app\index\model;

use think\Db;
class Articles
{
	/**
	 * 查询单个数据-find 方法查询结果不存在,返回 null,否则返回结果数组
	 */
	public function find(){
		// table方法必须指定完整的数据表名,必须带着表前缀
		$rs = Db::table('zht_articles')->where('articleId',100)->find();
		// name方法自动调用配置文件的表前缀,如果你的数据表没有设置表前缀的话,那么name和table方法效果一致。
		$rs2 = Db::name('articles')->where('articleId',1)->find();
		// 助手函数db
		$rs3 = db('articles')->where('articleId',1)->find();
		// 助手函数db-使用不同的数据库连接db_config1
		$rs4 = db('articles','db_config1')->where('articleId',100)->find();
		// 字符串方式配置数据库连接,可能无法定义某些参数,例如前缀和连接参数。
		$rs5 = db('zht_articles','db_config2')->where('articleId',101)->find();
		// 跟$rs4等效,动态连接数据库的connect方法仅对当次查询有效。
		$rs6 = Db::connect('db_config1')->table('zht_articles')->where('articleId',100)->find();
		dump($rs6);
		exit;
		//echo Db::getlastsql(); //获得最后一条sql语句
		return $rs;
	} 
	/**
	 * 查询单个数据-如果没有查找到数据,则会抛出一个think\db\exception\DataNotFoundException异常
	 */
	public function findOrFail(){
		$rs = Db::table('zht_articles')->where('articleId',100)->findOrFail();
		return $rs;
	}
	/**
	 * 查询单个数据-当查询不存在的时候返回空数组而不是Null
	 */
	public function findOrEmpty(){
		$rs = Db::table('zht_articles')->where('articleId',100)->findOrEmpty(); 
		return $rs;
	}
	/**
	 * 查询多个数据-select 方法查询结果是一个二维数组,如果结果不存在,返回空数组
	 */
	public function select(){
		$rs = Db::table('zht_articles')->where('dataFlag',1)->select();
		$rs2 = Db::name('articles')->where('dataFlag',1)->select();
		$rs3 = db('articles')->where('dataFlag',1)->select();
		dump($rs3);
		exit;
		return $rs;
	}
	/**
	 * 查询多个数据-如果没有查找到数据,同样也会抛出一个think\db\exception\DataNotFoundException异常
	 */
	public function selectOrFail(){
		$rs = Db::table('zht_articles')->where('dataFlag',-2)->selectOrFail();
		return $rs;
	}
	/**
	 * 值和列查询-查询某个字段的值可以用
	 */
	public function value(){
		// 查询某个字段的值可以用value,value 方法查询结果不存在,返回 null
		// 返回某个字段的值
		$rs = Db::table('zht_articles')->where('articleId',1)->value('articleTitle');
		// 查询某一列的值可以用,column 方法查询结果不存在,返回空数组
		// 返回数组
		$rs2 = Db::table('zht_articles')->where('dataFlag',1)->column('articleTitle');
		// 指定id字段的值作为索引
		$rs3 = Db::table('zht_articles')->where('dataFlag',1)->column('articleTitle','articleId');
//返回,使用的时候不用$rs3[$articleId]['articleTitle'],直接使用$rs3[$articleId]
//array(1) {
//  [1] => string(18) "商机内容测试"
//}
		// 指定id字段的值作为索引 返回所有数据
		$rs4 = Db::table('zht_articles')->where('dataFlag',1)->column('*','articleId');
		dump($rs4);
//返回,使用的时候用$rs3[$articleId]['articleTitle']
//array(5) {
//  [4] => array(3) {
//    ["customerId"] => int(4)
//    ["customerTel"] => string(11) "15012345678"
//    ["customerEmail"] => NULL
//  }
//  [5] => array(3) {
//    ["customerId"] => int(5)
//    ["customerTel"] => string(11) "13012341234"
//    ["customerEmail"] => NULL
//  }
//}
		exit;
		return $rs;
	}
	/**
	 * 大批量数据处理
	 */
	public function cursor(){
		// 如果你需要处理大量的数据,可以使用新版提供的游标查询功能,该查询方式利用了PHP的生成器特性,可以大幅减少大量数据查询的内存占用问题。-cursor方法返回的是一个生成器对象,user变量是数据表的一条数据(数组)。
		$cursor = Db::table('zht_articles')->where('dataFlag', 1)->cursor();
		foreach($cursor as $user){
			echo $user['articleTitle']."<br>";
		}
		exit;
		return $rs;
	}
}

版权声明:本篇文章来源于网络。 来源链接

相关推荐
Non-static method think\Config::get() should not be called statically
ThinkPHP5.1 | 2018-12-29 10393
原来是这样use think\Config; 改成这样use think\facade\Config; 下面是官方手册的解释 配置获取 要使用Config类,首先需要在你的类文件中引入 use think\facade\Config; 或者(因为系统做了类库别名,其实就是调用think\facade\Config) u...
thinkPHP5 order多条件排序
ThinkPHP5.1 | 2017-10-24 10262
总结如下: //单一条件排序 $user = $this->where(['parentId'=>0)->field('userId,userName,userSort,isShow')->order('userSort', 'asc')->select(); //多个条件排序,可以多加一个order...
评论:0条
评论加载中...
发表评论