HEX
Server: Apache/2.4.59 (Debian)
System: Linux keymana 4.19.0-21-cloud-amd64 #1 SMP Debian 4.19.249-2 (2022-06-30) x86_64
User: lijunjie (1003)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/dk/wp-content/themes/food/Model/CommonModel.php
<?php

class CommonModel
{
    public function getCountCommon($tableName, $where, $wheredata, $where2 = "1", $wheredata2 = "1", $debug = false)
    {
        global $wpdb;
        $sql = "SELECT COUNT(*) FROM {$tableName} WHERE {$where} = '%s' AND {$where2} = '%s' AND `status` != 99";
        $sql = $wpdb->prepare($sql, $wheredata, $wheredata2);

        if ($debug) {
            echo '<br>' . $sql;
        }
        return $wpdb->get_var($sql);
    }

    public function getCountCommon2($tableName, $where, $wheredata, $where2 = "1", $wheredata2 = "1", $debug = false)
    {
        global $wpdb;
        $sql = "SELECT COUNT(*) FROM {$tableName} WHERE {$where} = '%s' AND {$where2} = '%s'";
        $sql = $wpdb->prepare($sql, $wheredata, $wheredata2);

        if ($debug) {
            echo '<br>' . $sql;
        }
        return $wpdb->get_var($sql);
    }

    //没有 status
    public function getCountCommon3($tableName, $where, $wheredata, $where2, $wheredata2, $debug = false)
    {
        global $wpdb;
        $sql = "SELECT COUNT(*) FROM {$tableName} WHERE {$where} = '%s' AND {$where2} = '%s'";
        $sql = $wpdb->prepare($sql, $wheredata, $wheredata2);
        if ($debug) {
            echo '<br>' . $sql;
        }
        return $wpdb->get_var($sql);
    }

    public function getMaxIdCommon($tableName, $where, $whereData, $where2 = "1", $whereData2 = "1", $debug = false)
    {
        global $wpdb;
        $sql = "SELECT MAX(id) FROM {$tableName} WHERE {$where} = '%s' AND {$where2} = '%s' AND `status` != 99";
        $sql = $wpdb->prepare($sql, $whereData, $whereData2);

        if ($debug) {
            echo '<br>' . $sql;
        }
        return $wpdb->get_var($sql);
    }

    public function getInfoCommon($tableName, $where, $wheredata, $where2, $wheredata2, $debug = false)
    {

        global $wpdb;
        $sql = "SELECT * FROM {$tableName} WHERE {$where} = '%s' AND {$where2} = '%s' AND `status` != 99";

        $sql = $wpdb->prepare($sql, $wheredata, $wheredata2);
        if ($debug) {
            echo '<br>' . $sql;
        }
        return $wpdb->get_results($sql);
//        $info = $this->outputArr($re);

    }

    public function getInfoCommonNotStatus($tableName, $where, $wheredata, $where2, $wheredata2, $debug = false)
    {

        global $wpdb;
        $sql = "SELECT * FROM {$tableName} WHERE {$where} = '%s' AND {$where2} = '%s'";
        $sql = $wpdb->prepare($sql, $wheredata, $wheredata2);
        if ($debug) {
            echo '<br>' . PHP_EOL . $sql;
        }
        return $wpdb->get_results($sql);
    }

    public function getInfoCommon2($tableName, $where, $wheredata, $where2, $wheredata2, $debug = false)
    {

        global $wpdb;
        $sql = "SELECT * FROM {$tableName} WHERE {$where} = '%s' AND {$where2} != '%s' AND `status` != 99";

        $sql = $wpdb->prepare($sql, $wheredata, $wheredata2);
        if ($debug) {
            echo '<br>' . $sql;
        }
        $re = $wpdb->get_results($sql);
//        $info = $this->outputArr($re);
        return $re;
    }

    public function getSingleCommon($tableName, $value, $where, $wheredata, $where2, $wheredata2, $debug = false)
    {
        global $wpdb;
        $sql = "SELECT {$value} FROM {$tableName} WHERE {$where} = '%s'  AND {$where2} = '%s' AND `status` != 99";

        $sql = $wpdb->prepare($sql, $wheredata, $wheredata2);
        if ($debug) {
            echo '<br>' . $sql;
        }
        return $wpdb->get_var($sql);
    }

    public function getInfoINCommon($tableName, $where, $idlist, $debug = false)
    {

        global $wpdb;
        $sql = "SELECT * FROM {$tableName} WHERE {$where}  IN ({$idlist}) ";

//        $sql = $wpdb->prepare($sql, $idlist);

        if ($debug) {
            echo '<br>' . $sql;
        }
        $re = $wpdb->get_results($sql);
        return $re;
    }

    /**
     * @param $tableName
     * @param $where
     * @param $idArr
     * @param false $debug
     * @return mixed
     */
    public function getInfoINCommon2($tableName, $where, $idArr, $debug = false)
    {
        $idStr = implode(',', array_unique($idArr));

        global $wpdb;
        $sql = "SELECT * FROM {$tableName} WHERE {$where}  IN ({$idStr}) ";

        if ($debug) {
            echo '<br>' . $sql;
        }
        return $wpdb->get_results($sql);
    }

//    private function outputArr($re, $isObj = false)
//    {
//        $info = array();
//        if ($isObj) {
//            foreach ($re as $b) {
//                $outObj = new stdClass();
//                $outObj->id = $b->id;
//
//                $outObj->postid = intval($b->postid);
//                $outObj->postmoney = $b->postmoney;
//
//                $info[] = $outObj;
//            }
//        } else {
//            foreach ($re as $b) {
//                $info['id'][] = $b->id;
//                $info['postid'][] = $b->postid;
//                $info['postmoney'][] = $b->postmoney;
//            }
//        }
//        return $info;
//    }
//
//    private function outputIdArr($re, $isObj = false){
//        $info = array();
//        if ($isObj) {
//            foreach ($re as $b) {
//                $outObj = new stdClass();
//                $outObj->id = $b->id;
//
//                $outObj->postid = intval($b->postid);
//                $outObj->postmoney = $b->postmoney;
//
//                $info[$b->id] = $outObj;
//            }
//        } else {
//            foreach ($re as $b) {
//                $info['id'][$b->id] = $b->id;
//                $info['postid'][$b->id] = $b->postid;
//                $info['postmoney'][$b->id] = $b->postmoney;
//            }
//        }
//        return $info;
//    }


    public function getInfoByPageCommon($tableName, $where, $wheredata, $where2 = "1", $wheredata2 = "1", $pageLimit = 10, $startpoint = 0, $order = 'DESC', $boolLike = false, $debug = false)
    {
        global $wpdb;

        $sql = "SELECT * FROM {$tableName} WHERE {$where} = '%s' AND `status` != 99  AND ";
        if ($boolLike) {
            $sql .= "{$where2} LIKE '%{$wheredata2}%' ";
        } else {
            $sql .= "{$where2} = '{$wheredata2}' ";
        }
        $sql .= "ORDER BY  {$order} LIMIT %d,%d";
        $sql = $wpdb->prepare($sql, $wheredata, $startpoint, $pageLimit);

        if ($debug) {
            echo '<br>' . $sql;
        }

        $re = $wpdb->get_results($sql);
//        $info = $this->outputArr($re);
        return $re;
    }

    public function getCountByPageCommon($tableName, $where, $wheredata, $where2 = "1", $wheredata2 = "1", $pageLimit = 10, $startpoint = 0, $order = 'DESC', $boolLike = false, $debug = false)
    {
        global $wpdb;
        $sql = "SELECT COUNT(*) FROM {$tableName} WHERE {$where} = '%s' AND `status` != 99 AND ";
        if ($boolLike) {
            $sql .= "{$where2} like '%{$wheredata2}%'";
        } else {
            $sql .= "{$where2} = '{$wheredata2}'";
        }
        $sql = $wpdb->prepare($sql, $wheredata);

        if ($debug) {
            echo '<br>' . $sql;
        }
        $re = $wpdb->get_var($sql);
        return $re;
    }

    /**
     * 分页2
     * @param $tableName
     * @param $data
     * @param false $debug
     * @return mixed
     */
    public function getCountByPageCommon2($tableName, $data, $debug = false)
    {
        global $wpdb;
        $sql = "SELECT COUNT(*) FROM {$tableName} WHERE `status` != 99 AND";

        $sql .= $this->pageWhere($data, $debug);

        if ($debug) {
            echo '<br>' . PHP_EOL . $sql;
        }
        return $wpdb->get_var($sql);
    }

    public function getInfoByPageCommon2($tableName, $data, $debug = false)
    {
        global $wpdb;
        $sql = "SELECT * FROM {$tableName} WHERE `status` != 99 AND ";

        $sql .= $this->pageWhere($data, $debug);
        $sql .= " ORDER BY {$data->order} LIMIT $data->startpoint,$data->pageLimit";

        if ($debug) {
            echo '<br>' . $sql;
        }

        return $wpdb->get_results($sql);
//        return $this->outputArr($re);
    }

    //页面where
    private function pageWhere($data, $debug = false)
    {
        global $wpdb;
        $sql = '';
        if ($data->where1Like) {
            $sql .= " {$data->where} like '%{$data->whereData}%' ";
        } else {
            $sql1 = " {$data->where} = '%s' ";
            $sql .= $wpdb->prepare($sql1, $data->whereData);
        }

        if (isset($data->where2)) {
            if ($data->where2Like) {
                $sql .= " AND  {$data->where2} like '%{$data->whereData2}%' ";
            } else {
                $sql2 = " AND {$data->where2} = '%s' ";
                $sql .= $wpdb->prepare($sql2, $data->whereData2);
            }
        }
//        echo '$sql -> '.$sql;
        return $sql;
    }

    public function delCommon($tableName, $data, $debug = false)
    {
        global $wpdb;
        $sql = "UPDATE {$tableName} SET `status`='%d',`mtime`='%d' WHERE `id`='%d'";
        $sql = $wpdb->prepare($sql, 99, time(), $data->id);
        if ($debug) {
            echo '<br>' . $sql;
        }
        $re = $wpdb->query($sql);
        return $re;
    }


    /**
     * 分页
     * @param $sql
     * @param $data
     * @param false $debug
     * @return string
     */
    public function sqlStrByPage($sql, $data, $debug = false)
    {
        $outStr = '';
        $index = 0;
        if (is_array($data)) {
            foreach ($data as $item => $value) {
                if ($index == 0) {
                    if (check_str($sql, 'WHERE')) {
                        $outStr = $outStr . " AND ";
                    } else {
                        $outStr = $outStr . " WHERE ";
                    }
                    $outStr .= " $item = $value";
                } else {
                    $outStr .= " AND $item = $value";
                }
                $index++;
            }
        }

        $sql = $sql . $outStr;
//        if ($debug) {
//            print_r([]);
//            echo $sql;
//        }

        $outStr2 = '';
        if (check_str($sql, 'WHERE')) {
            $outStr2 = " AND ";
        } else {
            $outStr2 = " WHERE ";
        }
        $outStr2 = $outStr2 . "`status` != 99";

        return $sql . $outStr2;
    }

    /**
     * 序列化 输出字符串
     * @param $value
     * @return mixed|string
     */
    public function unserializeStrFunc($value)
    {
        return (strlen($value) > 3) ? unserialize($value) : '';
    }



    //        $status = $wpdb->insert($this->tableName,array(
//            'f_name' => "$data->f_name",
//            "s_name" => $data->s_name,
//            "cat" => $data->cat,
//            "remark" => $data->remark,
//        ),array('%s','%s','%s','%s'));
//        $wpdb->insert($this->tableName, array('column1' => 'value1', 'column2' => 123 ), array('%s','%s','%s','%s') );
////        if ($debug) {
//            echo '<br>'.PHP_EOL . $wpdb->last_query;
////        }
//        if($status){
//            $last_id = $wpdb->insert_id;
//        }
//        echo 'wp $last_id == '. $last_id;
//        return $last_id;

    /**
     * 事务
     */
//事务能够用来管理insert 、update 、delete语句。 一般事务必须满足的4个条件:原子性(Atomicity,或称不可分割性)、一致性(Consistency)、隔离性(Isolation,又称独立性)、持久性(Durability)。 事务简单来说就是做一件事要么成功,要么全部完成,要么全部不完成。事务在执行过程中发生错误,会被回滚(Rollback)到事务开始前的状态,就像这个事务从来没有执行过一样。
//
//###MySQL事务处理用 BEGIN, ROLLBACK, COMMIT来实现 1、BEGIN 开始一个事务 2、ROLLBACK 事务回滚 3、COMMIT 事务确认
//
//###code
//
//global $wpdb;
//
//// 开启事务
//$wpdb->query('START TRANSACTION');
//$user = array(
//'user_name' =>  'sample_password',
//'sex' => '1',
//'age' => '22'
//);
//
//for($i = 0;$i < 100;$i++){
//$userArray[$i] = $user;
//}
//$userArray[100] = array(
//    'user_name' =>  'sample_password',
//    'age' => '22'
//);
//foreach ($userArray as $index => $value){
//    $status = $wpdb->insert("test_transaction",$value);
//    if(!$status){
//        $wpdb->query('ROLLBACK'); //事务回滚
//    }
//}
//$wpdb->query('COMMIT'); //事务提交
}