-
Notifications
You must be signed in to change notification settings - Fork 348
/
Copy pathdb_functions.php
91 lines (81 loc) · 2.53 KB
/
db_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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
/***************************************
* http://www.program-o.com
* PROGRAM O
* Version: 2.0.1
* FILE: library/db_functions.php
* AUTHOR: ELIZABETH PERREAU
* DATE: MAY 4TH 2011
* DETAILS: common library of db functions
***************************************/
/**
* function db_open()
* Connect to the database
* @param string $host - db host
* @param string $user - db user
* @param string $password - db password
* @param string $database_name - db name
* @return resource $con - the database connection resource
**/
function db_open() {
global $dbh, $dbu, $dbp, $dbn, $dbPort;
$host = (!empty($dbPort) and $dbPort != 3306) ? "$dbh:$dbPort" : $dbh; // add port selection if not the standard port number
$conn = mysql_connect($host, $dbu, $dbp) or sqlErrorHandler( "mysql_connect", mysql_error(), mysql_errno());
$x = mysql_select_db($dbn) or sqlErrorHandler( "mysql_select_db", mysql_error(), mysql_errno());
return $conn;
}
/**
* function db_close()
* Close the connection to the database
* @param resource $con - the open connection
**/
function db_close($con) {
$discdb = mysql_close($con) or sqlErrorHandler( "mysql_close", mysql_error(), mysql_errno());
}
/**
* function db_query()
* Run a query on the db
* @param resource $con - the open connection
* @param string $sql - the sql query to run
* @return resource $result - the result resource
**/
function db_query($sql,$dbconn){
//run query
$result = mysql_query($sql,$dbconn)or sqlErrorHandler($sql, mysql_error(), mysql_errno());
//if no results output message
if(!$result){
}
//return result resource
return $result;
}
/**
* function db_make_safe()
* Makes a str safe to insert in the db
* @param string $str - the string to make safe
* @return string $str - the safe string
**/
function db_make_safe($str){
$dbconn = db_open();
$out = mysql_real_escape_string($str, $dbconn); // Takes into account the character set of the chosen database
mysql_close($dbconn);
return $out;
}
/**
* function db_res_count()
* Makes a str safe to insert in the db
* @param resource $result - the result resource
* @return int $res - the number of results
**/
function db_res_count($result){
return mysql_num_rows($result);
}
/**
* function db_res_array()
* returns an array of rows from the result
* @param resource $result - the result resource
* @return array $row - the array of results
**/
function db_res_array($result){
return mysql_fetch_array($result);
}
?>