Home > php教程 > php手册 > body text

php database class suitable for mysql sql service

WBOY
Release: 2016-10-15 10:31:47
Original
1631 people have browsed it

BaseModel basic Model class. Other database table files are based on this class. The current link is the database of sql service. For mysql, you only need to modify the query and execute

<?<span style="color: #000000;">php
</span><span style="color: #008000;">/*</span><span style="color: #008000;">*
 * 数据库基础类
 * @author ***
 </span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">abstract</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> BaseModel
{
    </span><span style="color: #0000ff;">static</span> <span style="color: #800080;">$model</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$sql</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$PrimaryKeyField</span>; <span style="color: #008000;">//</span><span style="color: #008000;">主键字段名</span>
    <span style="color: #0000ff;">private</span> <span style="color: #800080;">$field_values</span>;<span style="color: #008000;">//</span><span style="color: #008000;">存放数据库字段数组</span>
    <span style="color: #0000ff;">protected</span> <span style="color: #800080;">$db</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">protected</span> <span style="color: #800080;">$pk</span>; <span style="color: #008000;">//</span><span style="color: #008000;">主键</span>
    <span style="color: #0000ff;">protected</span> <span style="color: #800080;">$table</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">protected</span> <span style="color: #800080;">$field</span> = '*'<span style="color: #000000;">;
    </span><span style="color: #0000ff;">protected</span> <span style="color: #800080;">$where</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">protected</span> <span style="color: #800080;">$orderby</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">protected</span> <span style="color: #800080;">$limit</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">protected</span> <span style="color: #800080;">$groupby</span><span style="color: #000000;">;
    
    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     * 初始化
     *
     * @global array $TmacConfig
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> __construct()
    {
        </span><span style="color: #800080;">$this</span>->db = <span style="color: #800080;">$this</span>-><span style="color: #000000;">getDB();
    }
    
    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> getDB()
    {
        </span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">empty</span>(self::<span style="color: #800080;">$model</span><span style="color: #000000;">)){
            </span><span style="color: #800080;">$mssql</span> = <span style="color: #0000ff;">new</span> Mssql(DB_DEFAULT_HOST, DB_DEFAULT_NAME, DB_DEFAULT_PASSWD,<span style="color: #000000;"> DB_DEFAULT_DATABASE);
            self</span>::<span style="color: #800080;">$model</span> = <span style="color: #800080;">$mssql</span>-><span style="color: #000000;">getInstance();
        }
        </span><span style="color: #0000ff;">return</span> self::<span style="color: #800080;">$model</span><span style="color: #000000;">;
    }
    
    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     * 设置SQL语句
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span> setSQL(<span style="color: #800080;">$sql</span><span style="color: #000000;">)
    {
        </span><span style="color: #800080;">$this</span>->sql = <span style="color: #800080;">$sql</span><span style="color: #000000;">;
    }

    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     * 获取SQL语句
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">function</span><span style="color: #000000;"> getSQL()
    {
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>-><span style="color: #000000;">sql;
    }

    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     * 设置field_values
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">function</span> setFieldValues(<span style="color: #0000ff;">array</span> <span style="color: #800080;">$field_values</span><span style="color: #000000;">)
    {
        </span><span style="color: #800080;">$this</span>->field_values = <span style="color: #800080;">$field_values</span><span style="color: #000000;">;
    }
    
    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     * 获取field_values
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> getFieldValues()
    {
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>-><span style="color: #000000;">field_values;
    }

    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     * 设置主键字段名
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">function</span> setPrimaryKeyField(<span style="color: #800080;">$PrimaryKeyField</span><span style="color: #000000;">)
    {
        </span><span style="color: #800080;">$this</span>->PrimaryKeyField = <span style="color: #800080;">$PrimaryKeyField</span><span style="color: #000000;">;
    }
    
    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     * 获取主键字段名
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> getPrimaryKeyField()
    {
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>-><span style="color: #000000;">PrimaryKeyField;
    }
    
    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     * 设置表名
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">function</span> setTable(<span style="color: #800080;">$table</span><span style="color: #000000;">)
    {
        </span><span style="color: #800080;">$this</span>->table = <span style="color: #800080;">$table</span><span style="color: #000000;">;
    }
    
    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     * 获取表名
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> getTable()
    {
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>-><span style="color: #000000;">table;
    }
    
    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     * 设置主键
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">function</span> setPk(<span style="color: #800080;">$pk</span><span style="color: #000000;">)
    {
        </span><span style="color: #800080;">$this</span>->pk = <span style="color: #800080;">$pk</span><span style="color: #000000;">;
    }
    
    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     * 获取主键
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">function</span><span style="color: #000000;"> getPk()
    {
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>-><span style="color: #000000;">pk;
    }

        </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     * 设置Fields
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">function</span> setFields(<span style="color: #800080;">$fields</span><span style="color: #000000;">)
    {
        </span><span style="color: #800080;">$this</span>->field = <span style="color: #800080;">$fields</span><span style="color: #000000;">;
    }
    
        </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     * 获取Fields
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">function</span><span style="color: #000000;"> getFields()
    {
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>-><span style="color: #000000;">field;
    }
    
    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     * 设置where条件
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">function</span> setWhere(<span style="color: #800080;">$where</span><span style="color: #000000;">)
    {
        </span><span style="color: #800080;">$this</span>->where = <span style="color: #800080;">$where</span><span style="color: #000000;">;
    }
    
    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     * 获取where条件
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">function</span><span style="color: #000000;"> getWhere()
    {
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>-><span style="color: #000000;">where;
    }
    
    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     * 设置Group
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">function</span> setGroupBy(<span style="color: #800080;">$groupby</span><span style="color: #000000;">)
    {
        </span><span style="color: #800080;">$this</span>->groupby = <span style="color: #800080;">$groupby</span><span style="color: #000000;">;
    }
    
    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     * 获取Group
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">function</span><span style="color: #000000;"> getGroupBy()
    {
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>-><span style="color: #000000;">groupby;
    }
    
    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     * 设置Order
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">function</span> setOrderBy(<span style="color: #800080;">$orderby</span><span style="color: #000000;">)
    {
        </span><span style="color: #800080;">$this</span>->orderby = <span style="color: #800080;">$orderby</span><span style="color: #000000;">;
    }
    
    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     * 设置Order
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">function</span><span style="color: #000000;"> getOrderBy()
    {
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>-><span style="color: #000000;">orderby;
    }
    
    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     * 设置条数
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">function</span> setLimit( <span style="color: #800080;">$limit</span><span style="color: #000000;"> )
    {
        </span><span style="color: #800080;">$this</span>->limit = <span style="color: #800080;">$limit</span><span style="color: #000000;">;
    }
    
    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     * 获取条数
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">function</span><span style="color: #000000;"> getLimit()
    {
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>-><span style="color: #000000;">limit;
    }
    
    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     * 根据主键获取
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">function</span><span style="color: #000000;"> getInfoByPk()
    {
        </span><span style="color: #800080;">$sql</span> = "select {<span style="color: #800080;">$this</span>->getFields()} "
            ."from {<span style="color: #800080;">$this</span>->getTable()} "
            ."where {<span style="color: #800080;">$this</span>->getPrimaryKeyField()}={<span style="color: #800080;">$this</span>->getPk()}"<span style="color: #000000;">;
        </span><span style="color: #800080;">$result</span> = <span style="color: #800080;">$this</span>->query(<span style="color: #800080;">$sql</span><span style="color: #000000;">);
        </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$result</span> != <span style="color: #0000ff;">NULL</span><span style="color: #000000;">){
            </span><span style="color: #800080;">$result</span> = <span style="color: #800080;">$result</span>[0<span style="color: #000000;">];
        }
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$result</span><span style="color: #000000;">;
    }
    
    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     * 根据where条件获取一条信息
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">function</span><span style="color: #000000;"> getOneByWhere()
    {
        </span><span style="color: #800080;">$sql</span> = "SELECT {<span style="color: #800080;">$this</span>->getFields()} "
                . "FROM {<span style="color: #800080;">$this</span>->getTable()} "
                . "WHERE {<span style="color: #800080;">$this</span>->getWhere()}"<span style="color: #000000;">;
        </span><span style="color: #800080;">$res</span> = <span style="color: #800080;">$this</span>->query( <span style="color: #800080;">$sql</span><span style="color: #000000;"> );
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$res</span>[0<span style="color: #000000;">];
    }
    
    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     * 根据where条件获取数组列表
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">function</span><span style="color: #000000;"> getListByWhere()
    {
        </span><span style="color: #800080;">$sql</span> = "SELECT "<span style="color: #000000;">;
        </span><span style="color: #0000ff;">if</span> ( <span style="color: #800080;">$this</span>->getLimit() != <span style="color: #0000ff;">null</span><span style="color: #000000;"> ) {
            </span><span style="color: #800080;">$line_str</span> = <span style="color: #800080;">$this</span>->getWhere() != <span style="color: #0000ff;">null</span> ? "AND " : "WHERE "<span style="color: #000000;">;
            </span><span style="color: #0000ff;">if</span> (<span style="color: #008080;">strpos</span>(<span style="color: #800080;">$this</span>->getLimit(), ',') !== <span style="color: #0000ff;">FALSE</span><span style="color: #000000;">){
                </span><span style="color: #0000ff;">list</span>(<span style="color: #800080;">$page</span>, <span style="color: #800080;">$count</span>) = <span style="color: #008080;">explode</span>(',', <span style="color: #800080;">$this</span>-><span style="color: #000000;">getLimit());
                </span><span style="color: #800080;">$page_str</span> = <span style="color: #800080;">$count</span>*(<span style="color: #800080;">$page</span>-1<span style="color: #000000;">);
                </span><span style="color: #800080;">$sql</span> .= "TOP <span style="color: #800080;">$count</span> "<span style="color: #000000;">;
            } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
                </span><span style="color: #800080;">$sql</span> .= "TOP {<span style="color: #800080;">$this</span>->getLimit()} "<span style="color: #000000;">;
            }
        }
        </span><span style="color: #800080;">$sql</span> .= "{<span style="color: #800080;">$this</span>->getFields()} "
                . "FROM {<span style="color: #800080;">$this</span>->getTable()} "<span style="color: #000000;">;
        </span><span style="color: #0000ff;">if</span> ( <span style="color: #800080;">$this</span>->getWhere() != <span style="color: #0000ff;">null</span><span style="color: #000000;"> ) {
            </span><span style="color: #800080;">$sql</span> .= "WHERE {<span style="color: #800080;">$this</span>->getWhere()} "<span style="color: #000000;">;
        }
        </span><span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$page_str</span>) && <span style="color: #800080;">$page_str</span> != <span style="color: #0000ff;">NULL</span><span style="color: #000000;">){
            </span><span style="color: #800080;">$line_str</span> = <span style="color: #800080;">$this</span>->getWhere() != <span style="color: #0000ff;">null</span> ? "AND " : "WHERE "<span style="color: #000000;">;
            </span><span style="color: #800080;">$sql</span> .= "{<span style="color: #800080;">$line_str</span>} {<span style="color: #800080;">$this</span>->getPrimaryKeyField()} not in (select top <span style="color: #800080;">$page_str</span> {<span style="color: #800080;">$this</span>->getPrimaryKeyField()} from {<span style="color: #800080;">$this</span>->getTable()}) "<span style="color: #000000;">;
        }
        </span><span style="color: #0000ff;">if</span> ( <span style="color: #800080;">$this</span>->getGroupby() != <span style="color: #0000ff;">null</span><span style="color: #000000;"> ) {
            </span><span style="color: #800080;">$sql</span> .= "GROUP BY {<span style="color: #800080;">$this</span>->getGroupby()} "<span style="color: #000000;">;
        }
        </span><span style="color: #0000ff;">if</span> ( <span style="color: #800080;">$this</span>->getOrderby() != <span style="color: #0000ff;">null</span><span style="color: #000000;"> ) {
            </span><span style="color: #800080;">$sql</span> .= "ORDER BY {<span style="color: #800080;">$this</span>->getOrderby()} "<span style="color: #000000;">;
        }
        </span><span style="color: #800080;">$res</span> = <span style="color: #800080;">$this</span>->query( <span style="color: #800080;">$sql</span><span style="color: #000000;"> );
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$res</span><span style="color: #000000;">;
    }
    
    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     * 根据where获取count
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">function</span><span style="color: #000000;"> getCountByWhere()
    {
        </span><span style="color: #800080;">$sql_count</span> = "SELECT COUNT(*) AS total FROM {<span style="color: #800080;">$this</span>->getTable()} "<span style="color: #000000;">;
        </span><span style="color: #0000ff;">if</span> ( <span style="color: #800080;">$this</span>->getWhere() != <span style="color: #0000ff;">null</span><span style="color: #000000;"> ) {
            </span><span style="color: #800080;">$sql_count</span> .= "WHERE " . <span style="color: #800080;">$this</span>-><span style="color: #000000;">getWhere();
        }
        </span><span style="color: #800080;">$count</span> = <span style="color: #800080;">$this</span>->query( <span style="color: #800080;">$sql_count</span><span style="color: #000000;"> );
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$count</span> != <span style="color: #0000ff;">NULL</span> ? <span style="color: #800080;">$count</span>[0]['total'] : 0<span style="color: #000000;">;
    }


    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     * 根据主键更新
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">function</span> updateByPk(<span style="color: #800080;">$fieldList</span><span style="color: #000000;">)
    {
        </span><span style="color: #800080;">$sql</span> = "UPDATE {<span style="color: #800080;">$this</span>->getTable()} SET "<span style="color: #000000;">;
        </span><span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$this</span>->getFieldValues() <span style="color: #0000ff;">as</span> <span style="color: #800080;">$key</span> => <span style="color: #800080;">$one</span><span style="color: #000000;">){
            </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$one</span> != <span style="color: #0000ff;">NULL</span><span style="color: #000000;">){
                </span><span style="color: #800080;">$sql</span> .= "<span style="color: #800080;">$key</span>='<span style="color: #800080;">$one</span>',"<span style="color: #000000;">;
            }
        }
        </span><span style="color: #800080;">$sql</span> = <span style="color: #008080;">rtrim</span>(<span style="color: #800080;">$sql</span>, ','<span style="color: #000000;">);
        </span><span style="color: #800080;">$sql</span> .= " WHERE {<span style="color: #800080;">$this</span>->getPrimaryKeyField()}='{<span style="color: #800080;">$this</span>->getPk()}'"<span style="color: #000000;">;
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>->execute(<span style="color: #800080;">$sql</span><span style="color: #000000;">);
    }
    
    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     * 根据WHERE更新
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">function</span> updateByWhere(<span style="color: #800080;">$fieldList</span><span style="color: #000000;">)
    {
        </span><span style="color: #800080;">$sql</span> = "UPDATE {<span style="color: #800080;">$this</span>->getTable()} SET "<span style="color: #000000;">;
        </span><span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$this</span>->getFieldValues() <span style="color: #0000ff;">as</span> <span style="color: #800080;">$key</span> => <span style="color: #800080;">$one</span><span style="color: #000000;">){
            </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$one</span> != <span style="color: #0000ff;">NULL</span><span style="color: #000000;">){
                </span><span style="color: #800080;">$sql</span> .= "<span style="color: #800080;">$key</span>='<span style="color: #800080;">$one</span>',"<span style="color: #000000;">;
            }
        }
        </span><span style="color: #800080;">$sql</span> = <span style="color: #008080;">rtrim</span>(<span style="color: #800080;">$sql</span>, ','<span style="color: #000000;">);
        </span><span style="color: #800080;">$sql</span> .= " {<span style="color: #800080;">$this</span>->getWhere()}"<span style="color: #000000;">;
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>->execute(<span style="color: #800080;">$sql</span><span style="color: #000000;">);
    }
    
    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     * 根据WHERE更新
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">function</span> insert(<span style="color: #800080;">$fieldList</span><span style="color: #000000;">)
    {
        </span><span style="color: #800080;">$sql_values</span> = ''<span style="color: #000000;">;
        </span><span style="color: #800080;">$sql</span> = "INSERT INTO {<span style="color: #800080;">$this</span>->getTable()} ("<span style="color: #000000;">;
        </span><span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$this</span>->getFieldValues() <span style="color: #0000ff;">as</span> <span style="color: #800080;">$key</span> => <span style="color: #800080;">$one</span><span style="color: #000000;">){
            </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$one</span> != <span style="color: #0000ff;">NULL</span><span style="color: #000000;">){
                </span><span style="color: #800080;">$sql</span> .= "<span style="color: #800080;">$key</span>,"<span style="color: #000000;">;
                </span><span style="color: #800080;">$sql_values</span> .= "'<span style="color: #800080;">$one</span>',"<span style="color: #000000;">;
            }
        }
        </span><span style="color: #800080;">$sql</span> = <span style="color: #008080;">rtrim</span>(<span style="color: #800080;">$sql</span>, ',').") VALUES (".<span style="color: #008080;">rtrim</span>(<span style="color: #800080;">$sql_values</span>, ',').")"<span style="color: #000000;">;
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>->execute(<span style="color: #800080;">$sql</span><span style="color: #000000;">);
    }
    
    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     * odbc query操作
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span> query(<span style="color: #800080;">$sql</span><span style="color: #000000;">)
    {
        </span><span style="color: #800080;">$this</span>->setSQL(<span style="color: #800080;">$sql</span><span style="color: #000000;">);
        </span><span style="color: #800080;">$data</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">();
        </span><span style="color: #800080;">$sql</span> = <span style="color: #008080;">iconv</span>('UTF-8', 'GBK', <span style="color: #800080;">$sql</span><span style="color: #000000;">);
        </span><span style="color: #0000ff;">if</span> (self::<span style="color: #800080;">$model</span> == <span style="color: #0000ff;">NULL</span><span style="color: #000000;">){
            </span><span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">Exception</span>("数据库连接失败"<span style="color: #000000;">);
        }
        </span><span style="color: #800080;">$result</span> = <span style="color: #008080;">odbc_do</span>(self::<span style="color: #800080;">$model</span>, <span style="color: #800080;">$sql</span><span style="color: #000000;">);
        </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$result</span> != <span style="color: #0000ff;">NULL</span><span style="color: #000000;">){
            </span><span style="color: #0000ff;">while</span>(<span style="color: #800080;">$res</span> = <span style="color: #008080;">odbc_fetch_array</span>(<span style="color: #800080;">$result</span><span style="color: #000000;">)){
                </span><span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$res</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$key</span> => <span style="color: #800080;">$one</span><span style="color: #000000;">){
                    </span><span style="color: #800080;">$res</span>[<span style="color: #800080;">$key</span>] = <span style="color: #008080;">iconv</span>('GBK', 'UTF-8', <span style="color: #800080;">$one</span><span style="color: #000000;">);
                }
                </span><span style="color: #800080;">$data</span>[] = <span style="color: #800080;">$res</span><span style="color: #000000;">;
            }
        }
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$data</span><span style="color: #000000;">;
    }
    
    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     * odbc execute
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span> execute(<span style="color: #800080;">$sql</span>, <span style="color: #800080;">$iconv</span> = <span style="color: #0000ff;">TRUE</span><span style="color: #000000;">)
    {
        </span><span style="color: #800080;">$this</span>->setSQL(<span style="color: #800080;">$sql</span><span style="color: #000000;">);
        </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$iconv</span><span style="color: #000000;">){
            </span><span style="color: #800080;">$sql</span> = <span style="color: #008080;">iconv</span>('UTF-8', 'GBK', <span style="color: #800080;">$sql</span><span style="color: #000000;">);
        }
        </span><span style="color: #0000ff;">if</span> (self::<span style="color: #800080;">$model</span> == <span style="color: #0000ff;">NULL</span><span style="color: #000000;">){
            </span><span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">Exception</span>("数据库连接失败"<span style="color: #000000;">);
        }
        </span><span style="color: #0000ff;">return</span> <span style="color: #008080;">odbc_exec</span>(self::<span style="color: #800080;">$model</span>, <span style="color: #800080;">$sql</span><span style="color: #000000;">);
    }


    </span><span style="color: #008000;">//</span><span style="color: #008000;">析构函数,自动关闭数据库,垃圾回收机制</span>
    <span style="color: #0000ff;">function</span><span style="color: #000000;"> __destruct() {
        </span><span style="color: #008080;">odbc_close</span>(self::<span style="color: #800080;">$model</span><span style="color: #000000;">);
    }

}



</span><span style="color: #008000;">//</span><span style="color: #008000;"> ++++++++++++++++++++++++++++++++++++++++++++++++
                // 自定义类库
                // mssql 链接类
// ++++++++++++++++++++++++++++++++++++++++++++++++
//一个普遍通用的PHP连接MYSQL数据库类</span>
<span style="color: #0000ff;">class</span><span style="color: #000000;"> Mssql {
    </span><span style="color: #0000ff;">static</span> <span style="color: #800080;">$conn_line</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$db_host</span>; <span style="color: #008000;">//</span><span style="color: #008000;">数据库主机</span>
    <span style="color: #0000ff;">private</span> <span style="color: #800080;">$db_user</span>; <span style="color: #008000;">//</span><span style="color: #008000;">数据库用户名</span>
    <span style="color: #0000ff;">private</span> <span style="color: #800080;">$db_pwd</span>; <span style="color: #008000;">//</span><span style="color: #008000;">数据库用户名密码</span>
    <span style="color: #0000ff;">private</span> <span style="color: #800080;">$db_database</span>; <span style="color: #008000;">//</span><span style="color: #008000;">数据库名</span>
            
    <span style="color: #0000ff;">function</span> __construct(<span style="color: #800080;">$db_host</span>, <span style="color: #800080;">$db_user</span>, <span style="color: #800080;">$db_pwd</span>, <span style="color: #800080;">$db_database</span><span style="color: #000000;">) {
        </span><span style="color: #800080;">$this</span>->db_host = <span style="color: #800080;">$db_host</span><span style="color: #000000;">;
        </span><span style="color: #800080;">$this</span>->db_user = <span style="color: #800080;">$db_user</span><span style="color: #000000;">;
        </span><span style="color: #800080;">$this</span>->db_pwd = <span style="color: #800080;">$db_pwd</span><span style="color: #000000;">;
        </span><span style="color: #800080;">$this</span>->db_database = <span style="color: #800080;">$db_database</span><span style="color: #000000;">;
    }

    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     * 单例模式处理数据库连接
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">function</span><span style="color: #000000;"> getInstance()
    {
        </span><span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">empty</span>(self::<span style="color: #800080;">$conn_line</span><span style="color: #000000;">)){
            </span><span style="color: #800080;">$connstr</span> = "Driver={SQL Server};Server=".<span style="color: #800080;">$this</span>->db_host.";Database=".<span style="color: #800080;">$this</span>-><span style="color: #000000;">db_database;
            self</span>::<span style="color: #800080;">$conn_line</span> = <span style="color: #008080;">odbc_connect</span>(<span style="color: #800080;">$connstr</span>, <span style="color: #800080;">$this</span>->db_user, <span style="color: #800080;">$this</span>-><span style="color: #000000;">db_pwd);
        }
        </span><span style="color: #0000ff;">return</span> self::<span style="color: #800080;">$conn_line</span><span style="color: #000000;">;
    }
}</span>
Copy after login

The Model class of the database table inherits the BaseModel class

<?<span style="color: #000000;">php
</span><span style="color: #008000;">/*</span><span style="color: #008000;">*
 * 游戏截图类
 * @author ***
 </span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">class</span> GameImagesModel <span style="color: #0000ff;">extends</span><span style="color: #000000;"> BaseModel
{
    </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$field_values</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">(
        </span>'game_id' => '',//key为数据库字段名
        'img_url' => '',
        'atime' => '',
        'add_user' => '',<span style="color: #000000;">
    );
    
    </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> __construct() {
        parent</span>::<span style="color: #000000;">__construct();
        </span><span style="color: #800080;">$this</span>->setTable('game_images'<span style="color: #000000;">);
        </span><span style="color: #800080;">$this</span>->setPrimaryKeyField('id'<span style="color: #000000;">);
    }
    
    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     * 字段处理函数
     * @param array $field_values
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">function</span> setFieldValues(<span style="color: #0000ff;">array</span> <span style="color: #800080;">$field_values</span><span style="color: #000000;">) {
        </span><span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$field_values</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$key</span> => <span style="color: #800080;">$one</span><span style="color: #000000;">){
            </span><span style="color: #0000ff;">if</span> (!<span style="color: #008080;">array_key_exists</span>(<span style="color: #800080;">$key</span>, <span style="color: #800080;">$this</span>-><span style="color: #000000;">field_values)){
                </span><span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">Exception</span>(<span style="color: #800080;">$key</span>."不存在"<span style="color: #000000;">);//判断前端传来的数据是否合理
            }
        }
        parent</span>::setFieldValues(<span style="color: #800080;">$field_values</span><span style="color: #000000;">);
    }
}</span>
Copy after login

Example:

<span style="color: #008000;">//</span><span style="color: #008000;"> 对于GameDetail表的操作</span>
<span style="color: #800080;">$detail_model</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> GameDetailModel();
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 查询</span>
<span style="color: #800080;">$detail_model</span>->setFields('DId'<span style="color: #000000;">);
</span><span style="color: #800080;">$detail_model</span>->setWhere("1=1"<span style="color: #000000;">);
</span><span style="color: #800080;">$detail_model</span>->setOrderBy("DId DESC"<span style="color: #000000;">);
</span><span style="color: #800080;">$detail_model</span>->setLimit("1"<span style="color: #000000;">);
</span><span style="color: #800080;">$insert_id</span> = <span style="color: #800080;">$detail_model</span>-><span style="color: #000000;">getListByWhere();

</span><span style="color: #008000;">//</span><span style="color: #008000;"> 更新 data的key需要和Model里面设置的对照 也就是数据库的字段</span>
<span style="color: #800080;">$data</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">(
        </span>'GameName' => <span style="color: #800080;">$name</span>,
        'Subject' => <span style="color: #800080;">$subject</span>,
        'Grade' => <span style="color: #800080;">$grade</span>,
        'Teach' => <span style="color: #800080;">$teach</span>,
        'Point' => <span style="color: #800080;">$point</span>,
        'IsFree' => <span style="color: #800080;">$free</span>,
        'Detail' => <span style="color: #800080;">$detail</span>,
        'AddTime' => <span style="color: #800080;">$_time</span><span style="color: #000000;">
);
</span><span style="color: #800080;">$detail_model</span>->setPk(<span style="color: #800080;">$id</span><span style="color: #000000;">);
</span><span style="color: #800080;">$detail_model</span>->updateByPk(<span style="color: #800080;">$detail_model</span>->setFieldValues(<span style="color: #800080;">$data</span><span style="color: #000000;">));


</span><span style="color: #008000;">//</span><span style="color: #008000;"> 对于GameImages表的操作
// 插入</span>
<span style="color: #800080;">$image_fields</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">(
        </span>'game_id' => <span style="color: #800080;">$id</span>,
        'img_url' => <span style="color: #800080;">$image_path</span>,
        'atime' => <span style="color: #800080;">$_time</span>,
        'add_user' => <span style="color: #800080;">$user_id</span>,<span style="color: #000000;">
);
</span><span style="color: #800080;">$images_model</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> GameImagesModel();
</span><span style="color: #800080;">$images_model</span>->insert(<span style="color: #800080;">$images_model</span>->setFieldValues(<span style="color: #800080;">$image_fields</span>));
Copy after login

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template