这里只是一个阅读笔记


1)简单的赋值

->where("id=:id", [
    'id' => 1
])
上面的程式等同于 id=1


2)AND查询
->where("id=:id and pack_name=:pack_name", [
    ':id' => 1,
    ':pack_name' => 'com.famigo.sandbox'
])
或者

->where([
    'and',
    'id=:id',
    'pack_name=:pack_name'
], [
    ':id' => 1,
    ':pack_name' => 'com.famigo.sandbox'
])
这两种程式等同于 id=1 AND pack_name='com.famigo.sandbox'

3)OR查询

->where("id=:id or pack_name=:pack_name", [
    ':id' => 1,
    ':pack_name' => 'com.famigo.sandbox'
])
或者
->where([
    'or',
    'id=:id',
    'pack_name=:pack_name'
], [
    ':id' => 1,
    ':pack_name' => 'com.famigo.sandbox'
])
这两种程式等同于 id=1 OR pack_name='com.famigo.sandbox'
4)AND OR 混合查询

->where([
    'and',
    'display=:display',
    [
        'or',
        'id=:id1',
        'id=:id2'
    ]
], [
    ':display' => 1,
    ':id1' => 1,
    ':id2' => 2
])
上面的程式等同于(display=1) AND ((id=1) OR (id=2))
5)IN查询

->where([
    'in', 'id', [1, 3, 5, 6]
])
上面程式等同于 id in (1, 3, 5, 6)
->where([
    'and',
    'display=:display',
    'lang=:lang',
    [
    'in', 'id', [1, 3, 5, 6]
    ]
], [
    ':display' => 1,
    ':lang' => 2
])
上面程式等同于 (display=1) AND (lang=2) AND (`id` IN (1, 3, 5, 6))
更为麻烦点的例子

->where([
    'or',
    [
        'and',
        'display=:display1',
        [
        'in', 'id', [1, 3, 5, 6]
        ]
    ],
    [
        'and',
        'display=:display2',
        [
        'in', 'id', [2, 4, 8, 9]
        ]
    ]
], [
    ':display1' => 1,
    ':display2' => 2,
])
上面程式等同于((display=1) AND (`id` IN (1, 3, 5, 6))) OR ((display=2) AND (`id` IN (2, 4, 8, 9)))
6)NOT IN 查询

->where([
    'not in', 'id', [1, 2, 4, 3]
])
上面程式等同于`id` NOT IN (1, 2, 4, 3)
复杂的使用方法和上述的IN是一样的,参考即可。

7)LIKE 查询

->where([
    'like', 'pack_name', '%sandbox%'
])
上面程式等同于`pack_name` LIKE '%sandbox%'
->where([
    'like', 'pack_name', [
        '%sandbox%',
        'com.famigo%'
    ]
])
上面程式等同于`pack_name` LIKE '%sandbox%' AND `pack_name` LIKE 'com.famigo%'

->where([
    'or like', 'pack_name', [
        '%sandbox%',
        'com.famigo%'
    ]
])
上面程式等同于`pack_name` LIKE '%sandbox%' OR `pack_name` LIKE 'com.famigo%'
->where([
    'or not like', 'pack_name', [
        '%sandbox%',
        'com.famigo%'
    ]
])
上面程式等同于`pack_name` NOT LIKE '%sandbox%' OR `pack_name` NOT LIKE 'com.famigo%'
->where([
    'not like', 'pack_name', [
        '%sandbox%',
        'com.famigo%'
    ]
])
上面程式等同于`pack_name` NOT LIKE '%sandbox%' AND `pack_name` NOT LIKE 'com.famigo%'

原文:
https://blog.csdn.net/liruxing1715/article/details/48575025

有时候我们需要在后台配置某个时间段做什么事情,那么人性化的配置一般是: 21:30-8:30 这样的配置,那么我们使用的时候怎么来判断当前的时间是否在这个时间段内呢

下面是我的一段参考代码,仅作参考:


$mp_lyEnable = '21-8:30'; //后台设置的时间段
$lyEnable = false; //客服是否在离线时段
if ($mp_lyEnable) {
    $mp_lyEnable = explode('-', $mp_lyEnable);
    if (count($mp_lyEnable) == 2) {
         @list($startHour, $startMinute) = explode(':', $mp_lyEnable[0]);
         @list($endHour, $endMinute) = explode(':', $mp_lyEnable[1]);
         if (is_null($startMinute)) $startMinute = 0;
         if (is_null($endMinute)) $endMinute = 59;
         $startTime = strtotime(sprintf(date('Y-m-d %\d:%\d:00'), $startHour, $startMinute));
         $endTime = strtotime(sprintf(date('Y-m-d %\d:%\d:00'), $endHour, $endMinute));
         if ($endHour < $startHour) {
             //跨天
             $endTime += 86400;
         }
         $now = time();
         if ($now > $startTime && $now < $endTime) $lyEnable = true;
         var_dump(date('Y-m-d H:i:s', $startTime), date('Y-m-d H:i:s', $endTime));
    }
}

注意这里使用的是yii 1.1版本
有时候我们要批量写入数据,同时还要去重复,如下的方法可能有用(数据库设置了唯一索引)

$this->db = new CDbConnection('mysql:host=dev.database.local;port=3306;dbname=xxx', 'xxx', 'xxx');

$data = [
    ['areaId'=>1, 'title'=>'text1'],
    ['areaId'=>2, 'title'=>'text2'],
    ['areaId'=>3, 'title'=>'text3'],
];

$builder = $this->db->schema->getCommandBuilder();
$command = $builder->createMultipleInsertCommand("{{ztags}}", $data);
$sql = $command->getText();
$sql = str_replace('INSERT INTO', 'INSERT IGNORE INTO', $sql);

$params = [];
foreach ($data as $i=>$d) {
    foreach ($d as $k=>$v) {
        $params[":{$k}_{$i}"] = $v;
    }
}

$this->db->createCommand($sql)->execute($params);

文档参考:

  1. https://www.yiiframework.com/doc/api/1.1/CDbCommandBuilder#createMultipleInsertCommand-detail
  2. https://www.yiiframework.com/doc/api/1.1/CDbCommand#getText-detail

从阿里的数据库RDB库mysqldump下来的文件里看到
SET @@GLOBAL.GTID_PURGED='ddd7ffa8-e648-11e7-bdc7-00163e04b878:1-72683802';

这样的东西,然后在自建的数据库(宝塔)中导入,报出了如下的错误
ERROR 1227 (42000) at line 18: Access denied; you need (at least one of) the SUPER privilege(s) for this operation

备份的时候也有提示:
Warning: A partial dump from a server that has GTIDs will by default include the GTIDs of all transactions, even those that changed suppressed parts of the database. If you don't want to restore GTIDs, pass --set-gtid-purged=OFF. To make a complete dump, pass --all-databases --triggers --routines --events.

当然你要导入备份文件的话,把报错那几行内容注掉可以正常导入的。

然后就好奇网上查了一下,看到这文章不错,这里记录一下:

https://blog.csdn.net/woailyoo0000/article/details/88981380

php 7.0.0 套餐图片不显示问题 preg_replace 使用了/e 修饰符 (虽然我从没有用过,这里记录一下)
使用 preg_replace_callback 替换

    /**
     * 给文本中的链接加链接
     * @param type $content
     * @return type
     */
    public static function addcontentlink($content) {
        //在处理之前,先要把a或img标签内的排除,先替换
        preg_match_all('/<a.*?href=".*?".*?>.*?<\/a>/i', $content, $linkList);
        $linkList = $linkList[0];
        $str = preg_replace('/<a.*?href=".*?".*?>.*?<\/a>/i', '<{link}>', $content);
        //提取替换出所有的IMG标签(统一标记<{img}>)
        preg_match_all('/<img[^>]+>/im', $content, $imgList);
        $imgList = $imgList[0];
        $str = preg_replace('/<img[^>]+>/im', '<{img}>', $str);
        $str = preg_replace_callback('/(https?:\/\/[a-z0-9;&#@=_~%\?\/\.\,\+\-\!\:]+)/i', function ($match) {
            return static::strip_link($match[0]);
        }, $str);
        if (strpos($str, "http") === FALSE) {
            $str = preg_replace_callback('/(www.[a-z0-9;&#@=_~%\?\/\.\,\+\-\!\:]+)/i', function ($match) {
                return static::strip_link($match[0]);
            }, $str);
        } else {
            $str = preg_replace('/([[:space:]()[{}])(www.[a-z0-9;&#@=_~%\?\/\.\,\+\-\!\:]+)/i', '\1<a href="http://\2" target=_blank rel=nofollow>\2</a>', $str);
        }
        //还原A统一标记为原来的A标签
        $arrLen = count($linkList);
        for ($i = 0; $i < $arrLen; $i++) {
            $str = preg_replace('/<{link}>/', $linkList[$i], $str, 1);
        }
        //还原IMG统一标记为原来的IMG标签
        $arrLen2 = count($imgList);
        for ($i = 0; $i < $arrLen2; $i++) {
            $str = preg_replace('/<{img}>/', $imgList[$i], $str, 1);
        }
        return $str;
    }