The php database class stops here! The last DB class script you will ever need.
Hi!. Check this php database classes!.
You can throw away all the others with these classes.
Whether your projects are large or small you can choose the one thats suits your requirements.
For sql you can use the mysqli database class or for small projects the sql lite database class.
Both if these database classes support every call to the database. From create to update.
So why dont you give it a try? Its easy to implement and I know it will speed up your webdev projects.
You can download it for free below. Please donate a dollar for me.
Its works great and its very efficient. I hope you can find it useful. Check it out below. If you have a comment message me
1: <?php
2: /* INPUT FILTER CLASS NOT INCLUDED
3: Example of input filter class include.
4:
5: $clean = new InputFilter;
6: $data="YOUR INPUT DATA";
7: $cleaned=$clean->process($data); */
8:
9:
10: ///for mySql include dbi.inc.php
11: include_once("dbi.inc.php");
12: /// for mysqlLite include sqlitedb.inc.php
13: /// include_once("sqlitedb.inc.php");
14:
15: $db=new DB();
16: $db->open();
17:
18:
19: $qry="CREATE TABLE mytable ( name varchar(255) default NULL, age int(3) unsigned default NULL)";
20: $db->query($qry);
21:
22: $qry="INSERT INTO mytable (name, age) VALUES ('harish', 23)";
23: $db->query($qry);
24: if($db->affectedRows())
25: echo "Row inserted Succesfully<br>";
26: $qry="INSERT INTO mytable (name, age) VALUES ('sriram', 23)";
27: $db->query($qry);
28: if($db->affectedRows())
29: echo "Row inserted Succesfully<br>";
30:
31: $qry="SELECT * FROM mytable";
32: $db->query($qry);
33:
34: $total_records=$db->numRows(); //gets the total records fetched ny query
35:
36: while($row=$db->fetchObject())
37: {
38: echo $row->name."<br>";
39: }
40:
41: /* OR we can use.
42:
43: while($row=$db->fetchArray())
44: {
45: echo $row[first_name]."<br>";
46: }
47: */
48:
49: ?>
50:
51: <?php
52: /// mysqli class
53:
54: class DB
55: {
56: ///Declaration of variables
57:
58: var $host = '';
59: var $user = '';
60: var $password = '';
61: var $database = '';
62:
63: var $conn=NULL;
64: var $result=false;
65: var $fields;
66: var $check_fields;
67: var $tbname;
68: var $addNewFlag=false;
69: ///End
70:
71: function DB($host="",$user="",$password="",$dbname="",$open=false)
72: {
73: if($host!="")
74: $this->host=$host;
75: if($user!="")
76: $this->user=$user;
77: if($password!="")
78: $this->password=$password;
79: if($dbname!="")
80: $this->database=$dbname;
81:
82: if($open)
83: $this->open();
84: }
85: function open($host="",$user="",$password="",$dbname="") //
86: {
87: if($host!="")
88: $this->host=$host;
89: if($user!="")
90: $this->user=$user;
91: if($password!="")
92: $this->password=$password;
93: if($dbname!="")
94: $this->database=$dbname;
95:
96: if($this->connect()===false) return false;
97: /*if($this->select_db()===false) return false;*/
98: return $this->conn;
99: }
100: function set_host($host,$user,$password,$dbname)
101: {
102: $this->host=$host;
103: $this->user=$user;
104: $this->password=$password;
105: $this->database=$dbname;
106: }
107: function affectedRows() //-- Get number of affected rows in previous operation
108: {
109: return @mysqli_affected_rows($this->conn);
110: }
111: function close()//Close a connection to a Server
112: {
113: return @mysqli_close($this->conn);
114: }
115: function connect() //Open a connection to a Server
116: {
117: if(is_object($this->conn))
118: return true;
119: // Choose the appropriate connect function
120:
121: $this->conn = @mysqli_connect($this->host, $this->user, $this->password,$this->database);
122:
123: // Connect to the database server
124: if(!is_object($this->conn))
125: return false;
126: else
127: return true;
128:
129: }
130: function select_db($dbname="") //Select a databse
131: {
132: if($dbname=="")
133: $dbname=$this->database;
134: return @mysqli_select_db($this->conn,$dbname);
135: }
136: function create_db($dbname="") //Create a database
137: {
138: if($dbname=="")
139: $dbname=$this->database;
140: return $this->query("CREATE DATABASE ".$dbname);
141: }
142: function drop_db($dbname="") //Drop a database
143: {
144: if($dbname=="")
145: $dbname=$this->database;
146: return $this->query("DROP DATABASE ".$dbname);
147: }
148: function error() //Get last error
149: {
150: if(mysqli_connect_errno()) return mysqli_connect_error();
151: return (mysqli_error($this->conn));
152: }
153: function errorno() //Get error number
154: {
155: if(mysqli_connect_errno()) return mysqli_connect_errno();
156: return mysql_errno($this->conn);
157: }
158: function query($sql = '') //Execute the sql query
159: {
160: $this->result = @mysqli_query($this->conn, $sql );
161: return $this->result;
162: }
163: function numRows($result=null) //Return number of rows in selected table
164: {
165: if(!is_object($result))
166: $result = $this->result;
167: return (@mysqli_num_rows($result));
168: }
169: function fieldName($field, $result=null)
170: {
171: /*if(!is_object($result))
172: $result = $this->result;
173: return (@mysqli_field_name($result,$field));*/
174:
175: // Need to be implemented
176: }
177:
178: function insertID()
179: {
180: return (@mysqli_insert_id($this->conn));
181: }
182:
183: function data_seek($arg1,$row=0) ///Move internal result pointer
184: {
185: if(is_object($arg1))
186: $result = $arg1;
187: else
188: $result = $this->result;
189:
190: if(!is_object($arg1) && !is_null($arg1))
191: $row = $arg1;
192:
193: return mysqli_data_seek($result,$row);
194: }
195:
196: function fetchRow($result=null)
197: {
198: if(!is_object($result))
199: $result = $this->result;
200: return (@mysqli_fetch_row($result));
201: }
202:
203: function fetchObject($result=null)
204: {
205: if(!is_object($result))
206: $result = $this->result;
207: return (@mysqli_fetch_object($result));
208: }
209: function fetchArray($arg1=null,$mode=MYSQLI_BOTH)
210: {
211: if(is_object($arg1))
212: $result = $arg1;
213: else
214: $result = $this->result;
215:
216: if(!is_object($arg1) && !is_null($arg1))
217: $mode = $arg1;
218:
219: return (@mysqli_fetch_array($result,$mode));
220: }
221: function fetchAssoc($result=null)
222: {
223: if(!is_object($result))
224: $result = $this->result;
225: return (@mysqli_fetch_assoc($result));
226: }
227: function freeResult($result=null)
228: {
229: if(!is_object($result))
230: $result = $this->result;
231: return (@mysqli_free_result($result));
232: }
233: function getSingleResult($sql)
234: {
235: $result = $this->query($sql);
236: $row = $this->fetchArray($result,MYSQLI_NUM);
237: $return=$row[0];
238: return $return;
239: }
240:
241: function addNew($table_name)
242: {
243: $this->fields=array();
244: $this->addNewFlag=true;
245: $this->tbname=$table_name;
246: }
247:
248: function edit($table_name)
249: {
250: $this->fields=array();
251: $this->check_fields=array();
252: $this->addNewFlag=false;
253: $this->tbname=$table_name;
254: }
255:
256: function update()
257: {
258: foreach($this->fields as $field_name=>$value)
259: {
260: if($value=='--DATE--')
261: $qry.=$field_name."=now(),";
262: else if(strtolower(trim($value))=='now()')
263: $qry.=$field_name."=now(),";
264: else
265: $qry.=$field_name."='".$value."',";
266: }
267: $qry=substr($qry,0,strlen($qry)-1);
268:
269: if($this->addNewFlag)
270: $qry="INSERT INTO ".$this->tbname." SET ".$qry;
271: else
272: {
273: $qry="UPDATE ".$this->tbname." SET ".$qry;
274: if(count($this->check_fields)>0 && is_array($this->check_fields))
275: {
276: $qry.=" WHERE ";
277: foreach($this->check_fields as $field_name=>$value)
278: $qry.=$field_name."='".$value."' AND ";
279: $qry=substr($qry,0,strlen($qry)-5);
280: }
281: else if(!empty($this->check_fields))
282: {
283: $qry.=" WHERE ".$this->check_fields." ";
284: }
285:
286: }
287: return $this->query($qry);
288: }
289: }
290: ?>
291:
292:
293:
294:
295: <?php
296:
297: /// sqlite 2 class
298: // sqlite3 use native class
299: $db = new SQLite3('dataform.db');
300: if($result != 'not an error'){
301: $res = $db->query('SELECT * FROM mytable');
302: $arr = $res->fetchArray();
303:
304: }
305:
306: class DB
307: {
308: // Connection parameters
309:
310: var $database = '';///database i.e ../database/first_step.db
311: var $persistent = false;
312:
313:
314: // Database connection handle
315: var $conn = NULL;
316:
317: // Query result
318: var $result = false;
319:
320: function DB()
321: {
322: }
323: function open()
324: {
325: // Choose the appropriate connect function
326: if ($this->persistent) {
327: $func = 'sqlite_popen';
328: } else {
329: $func = 'sqlite_open';
330: }
331:
332: // Connect to the MySQL server
333: $this->conn = $func($this->database,0666,$sqliteerror);
334: if (!$this->conn) {
335: return false;
336: }
337: return true;
338: }
339:
340: function close()
341: {
342: return (@sqlite_close($this->conn));
343: }
344:
345: function error()
346: {
347: return (sqlite_error_string(sqlite_last_error()));
348: }
349:
350: function query($sql = '')
351: {
352: $this->result = sqlite_query($sql, $this->conn);
353: return ($this->result != false);
354: }
355:
356: function affectedRows()
357: {
358: return (@sqlite_changes($this->conn));
359: }
360:
361: function numRows()
362: {
363: return (@sqlite_num_rows($this->result));
364: }
365: function fieldName($field)
366: {
367: return (@sqlite_field_name($this->result,$field));
368: }
369: function insertID()
370: {
371: return (@sqlite_last_insert_rowid($this->conn));
372: }
373:
374: function fetchObject()
375: {
376: $object=new stdClass;
377: $tmp_arr=sqlite_fetch_array($this->result,SQLITE_NUM);
378: if($tmp_arr!=false)
379: {
380: $i=0;
381: foreach($tmp_arr as $value)
382: {
383: $fieldName=sqlite_field_name($this->result,$i);
384: $object->$fieldName=$value;
385: $i++;
386: }
387: }
388: else
389: return false;
390: return $object;
391: }
392:
393: function fetchArray()
394: {
395: return (@sqlite_fetch_array($this->result));
396: }
397:
398: function fetchAssoc()
399: {
400: return (@sqlite_fetch_array($this->result,SQLITE_ASSOC));
401: }
402:
403: }
404: ?>