forked from solmvz/IASBS-Chat-Room
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabase.php
66 lines (55 loc) · 1.83 KB
/
database.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
abstract class database
{
private static function ConnectToDB()
{
global $server;
global $db_username;
global $db_password;
global $db_name;
$connection = mysqli_connect($server, $db_username, $db_password, $db_name);
if ($connection->connect_error)
{
die("Connection failed: " . $connection->connect_error);
}
return $connection;
}
private static function PrepareParameters($ParamTypes, $Parameters)
{
$inputArray[] = &$ParamTypes;
$j = count($Parameters);
$ParameterQuestionMarks = "";
for($i=0;$i<$j;$i++){
$inputArray[] = &$Parameters[$i];
$ParameterQuestionMarks.='?,';
}
return array("ParameterQuestionMarks"=>$ParameterQuestionMarks, "inputArray"=>$inputArray);
}
public static function ExecuteQuery($StoredProcedureName, $ParamTypes="", $Parameters=array())
{
$connection = database::ConnectToDB();
$tempParameters = database::PrepareParameters($ParamTypes, $Parameters);
$inputArray = $tempParameters["inputArray"];
$ParameterQuestionMarks = $tempParameters["ParameterQuestionMarks"];
$sql = "CALL ".$StoredProcedureName."(".substr($ParameterQuestionMarks, 0, -1).")";
if($stmt = mysqli_prepare($connection, $sql))
{
if($ParamTypes != "")
call_user_func_array(array($stmt, 'bind_param'), $inputArray);
try {
$stmt->execute();
$result = $stmt->get_result();
mysqli_stmt_close($stmt);
}
catch (Exception $err)
{
die($err->getMessage());
}
}
else
echo "Error!";
$connection->close();
return $result;
}
}
?>