-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstmt.php
77 lines (64 loc) · 1.94 KB
/
stmt.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
67
68
69
70
71
72
73
74
75
76
77
<?php
// This class is wrapping prepared statments so you can use
// them like any normal query.
// Made by g_o
class Stmt // Wrapping class for mysqli_stmt
{
var $stmt = null; // Stmt holder
function __construct($con, $statement, $params, $types=null) // Make a prepared statement using connection to DB, statement and params
{
if($types==null) { // If types aren't specified, specify it automatically!
$types=$this->getTypes($params);
}
if($this->stmt = $con->prepare($statement)) { // Prepare the statement
call_user_func_array(array($this->stmt, "bind_param"), array_merge(array($types), $params)); // Bind the parameter
$this->stmt->execute(); // Execute the prepared statement
$this->stmt->store_result();
}
else
die("Error trying to prepare statement: ".$con->error);
}
function __destruct() // Close the stmt
{
$this->close();
}
function getTypes($params) // Get param types for prepared statements
{
$table = array("string"=>"s", "integer"=>"i", "double"=>"d", "object"=>"b");
$res = array();
$count = 0;
foreach($params as &$var) {
$res[$count] = $table[gettype($var)];
$count++;
}
return implode("",$res);
}
function getNumRows() // Get statment num_rows variable
{
return $this->stmt->num_rows;
}
function fetchRow() // Thanks to Masterkitano from php.net!
{
if($this->stmt->num_rows>0)
{
$result = array();
$md = $this->stmt->result_metadata();
$params = array();
while($field = $md->fetch_field()) {
$params[] = &$result[$field->name];
}
call_user_func_array(array($this->stmt, 'bind_result'), $params);
$this->stmt->fetch();
return $result;
}
return null;
}
function close() // Close the prepared statement variable (mysqli_stmt_close())
{
if(isset($this->stmt)) {
$this->stmt->close();
$this->stmt = null;
}
}
}
?>