-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
66 lines (54 loc) · 1.69 KB
/
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
class sqdbc{
//set database connection variable
private static $dbc;
//set database connection method
private static function connection(){
if(!isset(SELF::$dbc)){
try{SELF::$dbc= new PDO('sqlite:users.db');
SELF::$dbc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);}
catch(PDOEXCEPTION $err){
//close the database and echo the error
SELF::dbclose();
echo'sorry can\'t connect because '.$err->getmessage();}
}return SELF::$dbc;
}
//close the database
public static function dbclose(){
//set PDO instance to null
SELF::$dbc= null;
}
// method for queries that don't return records
public static function execution($sql){
try{
//get the connection variable
$rec=SELF::connection();
$record=$rec->prepare($sql);
$record->execute();
}catch(PDOEXCEPTION $err){
//close the database and echo the error
SELF::dbclose();
echo'sorry can\'t execute demand because '.$err->getmessage();}
}
public static function dbFetchAll($sql){
try{
$dbch=SELF::connection();
$data= $dbch->prepare($sql);
$data->execute();
// Fetch result
$result=$data->fetchAll(PDO::FETCH_ASSOC);
}
catch(PDOEXCEPTION $err){
//close the database and echo the error
SELF::dbclose();
echo'sorry can\'t connect because '.$err->getmessage();}
//return result
return $result;
}
public static function testinput($input){
$input=trim($input);
$input=stripslashes($input);
$input=htmlspecialchars($input);
return $input;}
}
?>