Skip to content

Commit a062fd3

Browse files
authored
Update phpedb.php
1 parent 42f740e commit a062fd3

File tree

1 file changed

+52
-2
lines changed

1 file changed

+52
-2
lines changed

Diff for: source/phpedb.php

+52-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
/**
33
*
44
* @Name : phpedb.php
5-
* @Version : 1.0
5+
* @Version : 1.1
66
* @Programmer : Max
7-
* @Date : 2016-2019, 2019-07-10, 2020-02-26
7+
* @Date : 2016-2019, 2019-07-10, 2020-02-26, 2020-02-27
88
* @Released under : https://github.com/BaseMax/PHPEDB/blob/master/LICENSE
99
* @Repository : https://github.com/BaseMax/PHPEDB
1010
*
@@ -207,6 +207,56 @@ public function select($table,$clause=[],$after="")
207207
$this->error_page($e->getMessage());
208208
}
209209
}
210+
public function sum($table, $column, $clause=[]) {
211+
try
212+
{
213+
$sql = "select SUM(".$column.") FROM `".$this->db."`.`".$table."` ";
214+
$current=0;
215+
$count=count($clause);
216+
if(count($clause) > 0)
217+
{
218+
$sql.=" WHERE ";
219+
foreach($clause as $name=>$value)
220+
{
221+
$operator="=";
222+
$do="and";
223+
if(is_array($value))
224+
{
225+
$operator=$value[0];
226+
$do=$value[1];
227+
$value=$value[2];
228+
}
229+
$sql.=$name . " ". $operator ." ? ";
230+
if($current != $count-1)
231+
{
232+
$sql.=" ".$do." ";
233+
}
234+
$current++;
235+
}
236+
}
237+
$sql.=";";
238+
$stmt = $this->database->prepare($sql);
239+
$current_all=1;
240+
if(count($clause) > 0)
241+
{
242+
foreach($clause as $name=>$value)
243+
{
244+
if(is_array($value))
245+
{
246+
$value=$value[2];
247+
}
248+
$stmt->bindValue($current_all,$value,PDO::PARAM_STR);
249+
$current_all++;
250+
}
251+
}
252+
$stmt->execute();
253+
return $stmt->fetchColumn(0);
254+
}
255+
catch(PDOException $e)
256+
{
257+
$this->error_page($e->getMessage());
258+
}
259+
}
210260
public function count($table,$clause=[])
211261
{
212262
try

0 commit comments

Comments
 (0)