#1 hblzxsj
spModel中的delete, update函数目前是成功返回1, 是否这几项更新相关的函数应该返回本次操作影响记录数,另外在数据库驱动库中,没有对affected_rows的封装2010-04-26 21:52:28
2010-04-26 21:52:28
2010-04-26 22:00:59
public Function affectedRows()将exec函数修改,增加一个传入参数
{
if ($this->conn)
return mysql_affected_rows($this->conn);
else
return false;
} // end Function
/**再修改一下spModel.php的update函数
* 执行一个SQL语句
*
* @param sql 需要执行的SQL语句
*/
public function exec($sql, /*这个参数是新增的*/$Affectedrows = false)
{
$this->arrSql[] = $sql;
if( $result = mysql_query($sql, $this->conn) ){
return $Affectedrows ? $this->affectedRows() : $result;
}else{
spError("{$sql}
执行错误: " . mysql_error());
}
}
/**还有delete也同样修改
* 修改数据,该函数将根据参数中设置的条件而更新表中数据
*
* @param conditions 数组形式,查找条件,此参数的格式用法与find/findAll的查找条件参数是相同的。
* @param row 数组形式,修改的数据,
* 此参数的格式用法与create的$row是相同的。在符合条件的记录中,将对$row设置的字段的数据进行修改。
*/
public function update($conditions, $row)
{
$where = "";
$row = $this->__prepera_format($row);
if(empty($row))return FALSE;
if(is_array($conditions)){
$join = array();
foreach( $conditions as $key => $condition ){
$condition = $this->__val_escape($condition);
$join[] = "{$key} = '{$condition}'";
}
$where = "WHERE ".join(" AND ",$join);
}else{
if(null != $conditions)$where = "WHERE ".$conditions;
}
foreach($row as $key => $value){
$value = $this->__val_escape($value);
$vals[] = "{$key} = '{$value}'";
}
$values = join(", ",$vals);
$sql = "UPDATE {$this->tbl_name} SET {$values} {$where}";
return $this->_db->exec($sql, /*这里增加就可以了*/true);
}
/**这样就可以返回影响记录数了
* 按条件删除记录
*
* @param conditions 数组形式,查找条件,此参数的格式用法与find/findAll的查找条件参数是相同的。
*/
public function delete($conditions)
{
$where = "";
if(is_array($conditions)){
$join = array();
foreach( $conditions as $key => $condition ){
$condition = $this->__val_escape($condition);
$join[] = "{$key} = '{$condition}'";
}
$where = "WHERE ( ".join(" AND ",$join). ")";
}else{
if(null != $conditions)$where = "WHERE ( ".$conditions. ")";
}
$sql = "DELETE FROM {$this->tbl_name} {$where}";
return $this->_db->exec($sql, true);
}
2010-04-27 11:36:52
2010-04-27 12:08:33
2010-04-27 15:09:39
2010-06-01 14:25:30
2010-06-01 17:08:36