post
poster: jsaxton
description: SQL include file
language: PHP
[download]
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
    //We have decided to further abstract the DBX functions in order to ensure maximum compatibility
    //Inserts shall be done with q_insert_from_array
    //Updates shall be done with q_update_from_array
    //Selects shall be done with q_select
    //Deletes shall be done with q_general

    $dbh = dbx_connect(DBX_MYSQL, "host", "DatabaseName", $_SERVER['sqluser'], $_SERVER['sqlpass']);
    global $dbh;

    //Insert Data
    //This allows a user to supply a database handle, connection, and an array in order to 
    //insert data into a database
    //The array should be an associative of the format:
    //(field1->data1, field2->data2, ... fieldn->datan)
    function q_insert_from_array($dbh, $table, $in_array)
    {
        //  inserts data into a table from an associative array
        //  returns the auto-generated id for the table, or dies if the insert failed
        
        $keys = array_keys($in_array);
        
        $query1 = "INSERT INTO $table (";
        $query2 = " VALUES (";
        $comma = "";
        
        foreach ( $keys as $field )
        {        
            $query1 .= $comma . $field;
            $query2 .= $comma . "'" . dbx_escape_string($dbh, $in_array[$field]) . "'";
            $comma = ", ";
        }
        
        $query = $query1 . ")" . $query2 . ")";
        if ( dbx_query($dbh, $query) )
        {
            $result = dbx_query($dbh, "SELECT IDENT_CURRENT('" . $table . "')");
            $auto_id = $result->data[0][0];
            
            return $auto_id;        
        }
        else
        {
            echo $query."<br>";
            die("Sorry, the database is not responding at this time.  Line " . __FUNCTION__);
        }
    }
    
            
    //Update Data
    //This allows a user to supply a database handle, connection, id_field, id_field contents, and an array
    //in order to update a database
    //The array should be an associative of the format:
    //(field1->data1, field2->data2, ... fieldn->datan)
    function q_update_from_array($dbh, $table, $up_array, $id_field, $id)
    {
        //  updates table using data from associative array.
        //  id_field and id are the key field in the table, and the value of that key
        //        field to match in order to update only the proper record
        //    dies if the update failed.
        
        $query = "UPDATE $table SET ";
        $comma = "";
        
        $keys = array_keys($up_array);
    
        foreach ( $keys as $field )
        {        
            $query .= $comma . $field . " = '" . dbx_escape_string($dbh, $up_array[$field]) . "'";
            $comma = ", ";
        }
        
        $query .= " WHERE ($id_field = '$id')";
        
        if (!dbx_query($dbh, $query) )
        {
            die("Sorry, the database is not responding at this time.  Line " . __FUNCTION__);
        }
        return;
    }

    //Select Data
    //This allows a user to supply a database handle, connection, and array in order to insert data into a database
    //The array should be an associative of the format:
    //(field1->data1, field2->data2, ... fieldn->datan)
    //Note: One must make $q_rows global before calling q_select if they wish to access said variable
    function q_select($dbh, $query)
    {
        global $q_rows;
        $result = dbx_query($dbh, $query);
        if (!is_object($result) )
        {
            echo dbx_error($dbh);
            exit(1);
        }
        $q_rows = $result->rows;
        $r = $result->data;
        
        stripslashes_deep($r);
        
        return $r;
    }

    //Execute Stored Procedure
    function q_exec_sp($dbh, $query)
    {
        global $q_rows;
        $result = dbx_query($dbh, $query);
        if (!is_object($result) )
        {
            die("Sorry, the database is not responding at this time.  Line " . __FUNCTION__);
        }
        $r = $result->data;
        $q_rows = $result->rows;
        return $r;
    }

    //General SQL stuff
    //Provide a database handle and a query.
    //Simple enough.
    function q_general($dbh, $query)
    {
        // Use with delete, or insert and update when not from an array
        
        $result = dbx_query($dbh, $query);
        
        return $result;
    }
    
    //taken from php.net
    function stripslashes_deep($value){
        $value = is_array($value) ?
            array_map('stripslashes_deep', $value) :
            stripslashes($value);

        return $value;
    }
?>