<?php
/**
* Model
* @param type $data 数组
* @param type $mode 类型[INSERT/UPDATE]
* @param type $where 条件
* @return type
*/
public function addcomment($data, $mode = 'INSERT', $where = '')
{
$rs = $this->db->autoExecute($this->lxsline_table, $data, $mode, $where);
return $rs;
}
/**
* 取所有行的第一个字段信息
*
* @param string $sql SQL语句
* @return array
* @access public
*/
function getCol($sql)
{
$res = $this->query($sql);
if ($res !== false) {
$arr = array();
while ($row = mysql_fetch_row($res)) {
$arr[] = $row[0];
}
return $arr;
} else {
return false;
}
}
/**
* 操作语句封装
* insert update For Mysql
* @param <type> $table
* @param <type> $field_values
* @param <type> $mode
* @param <type> $where
* @return <type>
*/
function autoExecute($table, $field_values, $mode = 'INSERT', $where = '')
{
$field_names = $this->getCol('DESC ' . $table);
$sql = '';
if ($mode == 'INSERT') {
$fields = $values = array();
foreach ($field_names AS $value) {
if (array_key_exists($value, $field_values) == true) {
$fields[] = '`' . $value . '`';
$values[] = "'" . $field_values[$value] . "'";
}
}
if (!empty($fields)) {
$sql = 'INSERT INTO ' . $table . ' (' . implode(', ', $fields) . ') VALUES (' . implode(', ', $values) . ')';
}
} else {
$sets = array();
foreach ($field_names AS $value) {
if (array_key_exists($value, $field_values) == true) {
$sets[] = '`' . $value . "` = '" . $field_values[$value] . "'";
}
}
if (!empty($sets)) {
$sql = 'UPDATE ' . $table . ' SET ' . implode(', ', $sets) . ' WHERE ' . $where;
}
}
if ($sql) {
return $this->query($sql);
} else {
return false;
}
}