Every time I need to build a quick web application in PHP, I always use this simple database class. I made some modifications and added more functions to the original source of this class.
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
| class Database { var $Host = "localhost" ; // Hostname of our MySQL server. var $Database = "" ; // Logical database name on that server. var $User = "" ; // User and Password for login. var $Password = "" ; var $Link_ID = 0; // Result of mysql_connect(). var $Query_ID = 0; // Result of most recent mysql_query(). var $Record = array (); // current mysql_fetch_array()-result. var $Row ; // current row number. var $LoginError = "" ; var $Errno = 0; // error state of query... var $Error = "" ; //------------------------------------------- // Connects to the database //------------------------------------------- function connect() { if ( 0 == $this ->Link_ID ) $this ->Link_ID=mysql_connect( $this ->Host, $this ->User, $this ->Password ); if ( ! $this ->Link_ID ) $this ->halt( "Link-ID == false, connect failed" ); if ( !mysql_query( sprintf( "use %s" , $this ->Database ), $this ->Link_ID ) ) $this ->halt( "cannot use database " . $this ->Database ); } // end function connect //------------------------------------------- // Queries the database //------------------------------------------- function query( $Query_String ) { $this ->connect(); $this ->Query_ID = mysql_query( $Query_String , $this ->Link_ID ); $this ->Row = 0; $this ->Errno = mysql_errno(); $this ->Error = mysql_error(); if ( ! $this ->Query_ID ) $this ->halt( "Invalid SQL: " . $Query_String ); return $this ->Query_ID; } // end function query //------------------------------------------- // If error, halts the program //------------------------------------------- function halt( $msg ) { printf( " <strong>Database error:</strong> %s n", $msg ); printf( "<strong>MySQL Error</strong>: %s (%s) n", $this ->Errno, $this ->Error ); die ( "Session halted." ); } // end function halt //------------------------------------------- // Retrieves the next record in a recordset //------------------------------------------- function nextRecord() { @ $this ->Record = mysql_fetch_array( $this ->Query_ID ); $this ->Row += 1; $this ->Errno = mysql_errno(); $this ->Error = mysql_error(); $stat = is_array ( $this ->Record ); if ( ! $stat ) { @ mysql_free_result( $this ->Query_ID ); $this ->Query_ID = 0; } return $stat ; } // end function nextRecord //------------------------------------------- // Retrieves a single record //------------------------------------------- function singleRecord() { $this ->Record = mysql_fetch_array( $this ->Query_ID ); $stat = is_array ( $this ->Record ); return $stat ; } // end function singleRecord //------------------------------------------- // Returns the number of rows in a recordset //------------------------------------------- function numRows() { return mysql_num_rows( $this ->Query_ID ); } // end function numRows //------------------------------------------- // Returns the Last Insert Id //------------------------------------------- function lastId() { return mysql_insert_id(); } // end function numRows //------------------------------------------- // Returns Escaped string //------------------------------------------- function mysql_escape_mimic( $inp ) { if ( is_array ( $inp )) return array_map ( __METHOD__ , $inp ); if (! empty ( $inp ) && is_string ( $inp )) { return str_replace ( array ( '\\' , "\0" , "\n" , "\r" , "'" , ' "', " \x1a "), array('\\\\', '\\0', '\\n', '\\r', " \\' ", '\\" ', '\\Z' ), $inp ); } return $inp ; } //------------------------------------------- // Returns the number of rows in a recordset //------------------------------------------- function affectedRows() { return mysql_affected_rows(); } // end function numRows //------------------------------------------- // Returns the number of fields in a recordset //------------------------------------------- function numFields() { return mysql_num_fields( $this ->Query_ID); } // end function numRows } // end class Database /* From: kjventura.com */ |
Simple Implementation
After configuring the database class, save the code above as database.php
Single Record
1
2
3
4
5
6
7
8
9
10
11
12
13
| require_once ( "database.php" ); $db = new Database(); $sql = "SELECT * FROM tbl_user WHERE userId='1' " ; $db ->query( $sql ); $db ->singleRecord(); //call this if the query will only return a single row echo $db ->Record[ 'userId' ]; // use the field name for example or; echo $db ->Record[0]; //use indexes |
Multiple Records
1
2
3
4
5
6
7
8
9
10
11
12
13
| require_once ( "database.php" ); $db = new Database(); $sql = "SELECT * FROM tbl_user WHERE " ; $db ->query( $sql ); while ( $db ->nextRecord()){ echo $db ->Record[ 'userId' ]; } //will output all rows with the field of userId returned by the query |
Other Functions
1
2
3
4
5
6
7
| $db ->lastId(); // Returns the primary key of the last inserted record $db ->numRows(); //Returns the number of rows in a recordset $db ->numFields(); //Returns the number of fields in a recordset $db ->mysql_escape_mimic( $string ); // Returns escaped string |
0 comments:
Post a Comment