MySQL class

Every developer has been asked many time to retrieve or modify information from a database, most of the case the database is MySQL. For this reason I have created a class that use everytime I have to manage a database.

Continue reading below

In particular this class has two main files, the first one contains only constant and they are the useful constants that you need to use the database. You have to fill this file with your informations. The second file is the class, this class creates the elements you need to interface with the database.

It has the following functions:

  • Insert row:
    • $id = $database->insert(DBTABLE_NAME,fields array(),values array());
      • inputs:
        • DBTABLE_NAME, the variable you have set in the config file
        • fields array(), the fields you have to set
        • values array(), this array has to have the same length of the fields array, and also the same order
      • result can be zero if there have been an error, otherwise is the ID of the row inserted
  • Select row :
    • $array_data = $database->select(DBTABLE_NAME,fields array(), array(" WHERE field=\"value_field\"")) 
      • inputs:
        • DBTABLE_NAME, the variable you have set in the config file
        • fields array(), the fields you have to select, if the array is empty it will select all the fields
        • additional request array(” WHERE field=\”value_field\””), here you can use any additional code of mysql, if you don’t want to add leave an empty array, this array has to have only one element
      • result will be an array of array, to obtain the ID of the third element you have to write $array_data[2][“ID”]
  • Update row:
    • $result = $database->update(DBTABLE_NAME,fields array(),values array(),array(" WHERE field=field_value")) 
    • inputs:
      • DBTABLE_NAME, the variable you have set in the config file
      • fields array(), the fields you have to update
      • values array(), this array has to have the same length of the fields array, and also the same order
      • array(” WHERE id=1″), this array has to have one element and it has to be where the database has to modify
    • result can be 0 if there have been an error, 1 in case of success
  • Delete row:
    • $result = $database->delete(DBTABLE_NAME,fields array(),values array()) 
    • inputs:
      • DBTABLE_NAME, the variable you have set in the config file
        • fields array(), the fields where you have to delete the row
        • values array(), this array has to have the same length of the fields array, and also the same order
  • result is 1 in fase off success, otherwise 0

To use this class remember to include include the php file as in the following:

include_once("config.php");
include_once("databaseManager.php");
$database = new DatabaseManager();
You can download the code here.

And if you want punctual customization or you need a technician to add this functionality, we can help you, talk with us in many in WhatsApp or Skype.

Subscribe

Subscribe now and you will be updated for any news about the IoT word

2 thoughts on “MySQL class”

Leave a Reply

Your email address will not be published. Required fields are marked *

Elisabetta Cataldi

© Copyright 2023 - All Rights Reserved
It was a pleasure see you there, we will see again