Search

Sunday 18 July 2010

Database Updated Finally!

Hey Guys,

Finally I have added some nice database features which should give the class some meaning now, as previously it only ran your query (custom queries) and only did some simple validations for you.

Now it comes with some powerful mysql features and you can slowly try to build more OOP based design.

new!

You can now do the following most used features of mysql using this helper class:
1) Create Table
2) Insert data in table
3) Edit and update values
4) Remove items using a bit complex conditions
5) and last but not the least... Find!

Do the following before you attempt anything with these files:

Download database class: _here_

include("database.php");

$db = new DBConnect('host_name','database_name','user_name','password');
$con = $db->setDb();

Now to create table simply:



$db->createTable('contacts', array('name', 'address', 'phone', 'email'), array('varchar(30)', 'varchar(50)', 'varchar(12)', 'varchar(30)'), TRUE);

The function parameters are:
1) Table name - String
2) Field names - Array of String
3) Field types - Array of String
4) Overwrite - Boolean

The first three parameters are the obvious requirement for the mysql CREATE TABLE function. Make sure the number of 'Field names' and 'Field types' are equal. The final parameter is a boolean, if set true then it will overwrite any previous table with the same name. Set it to false for safety.

Now insert values to table!



$db->insert('contacts', array('name','address','phone','email'), array('Anas Nasarullah','Uttara Dhaka Bangladesh','9181819','anasrlh@hotmail.com'));

The parameters here are simple to tell. All of which is required by Mysql for INSERT function.

The function parameters are:
1) Table name - String
2) Field names - Array of String
3) Field values - Array of String

Now time to do some editing to the values!



$db->edit('contacts', 'email', 'anasarulh@hotmail.com', 'email', 'anasrlh@hotmail.com');

This function should have more functionalities to handle complex conditions. For the time being it only finds the value of the given field and value and changes that value.

The function parameters are:
1) Table name - String
2) Edit field name - String
3) New value - String
4) Find field name - String
5) Find value - String

Now we will remove an element from table



$db->remove('contacts', array('email', 'name'), array('anasarulh@hotmail.com', 'Anas Nasarullah'), 'or');

This function has powerful conditioning among all the functions we discussed till now. You can give a set of fields which should have a set of values and can tell if they will have 'and' or 'or' condition to choose the correct field. Even though the disadvantage is that all the conditions will be 'and-ed' or 'or-ed' but cannot be a mixture more complex possible conditions.

The function parameters are:
1) Table name - String
2) Find field names - Array of Strings
3) Find values - Array of Strings
4) Operator - String ('and' / 'or')

Finally its time to Find!



It is a very basic and simple find function as follows:

$db->find('contacts', 'name', 'Anas', 0);

The function can only for now take table name, the field to look for and the value to look for. The final flag parameter is boolean and it means that the value we search for, whether it is an exact search or we match with all values containing value string.

Flag = 1 means find with exact value.
Flag = 0 means find with string containing the value.

The function parameters are:
1) Table name - String
2) Find field name - String
3) Find value - String
4) Flag - Boolean (0 / 1)

Thats all folks!