Home » Programming » Creating a MySQL Database for a PHP Application: How to Automate the Creation of MySQL Databases with PHP

RSS Fardin in Bremen

  • 19th century Market on the Grand Place of Brussels June 14, 2009
    by: Ibu ClaireThe main market of Brussels has been used as a market, since its creation beginning 10th century.Vegetables and poultry were sold, but also birds maintained into cages for their singing...At the end of 17th century, King Louis the 14th of France willing to conquer the Low Countries(name of Belgium at that time), bombed […]
  • Manneken-Pis June 14, 2009
    by : Ibu ClaireManneken-Pis (a 100m. away from the Grand-Place)This little 17th C. boy is supposed to be the oldest inhabitant of the city of Brussels. He is supposed to have gotten lost, and when found back, he was in this positon !His family was so happy that they offered a water-fontain to the inhabitants […]
  • Place Royale Brussels May 30, 2009
    by: Ibu ClaireThis square was built on the ruins of a previous royal palace, that burned out around 1715. Around 1780, huge works started in the "classical" style inspired by the castle of Versailles (France). It was supposed to be used for military parades,as the new palace was built "around the corner"Archeological researches were made […]
  • Atomium of Brussels 1958 May 21, 2009
    by: Ibu ClaireWas erected in 1958 for the World Exhebition. It represents a molecule of cristal iron and symbolizes the 9 provinces of Belgium. Each sphere has a diameter of 18 m.It is 102m. high and weighs 2400 tons. The designer was Ingenior André Waterkeyn.For its 5Oth anniversary, it was completely restaured,with partly by private […]

RSS Laravel

  • Practical and Helpful Tips:
    Laravel Practical and Helpful Tips: Picking the Most Excellent Drywall Repair Company The drywall is an integral component of your house because it provides some insulation, which leads to high energy efficiency. Besides, you can improve the beauty of your home when you maintain the drywall in the right way. The drywall might destruct after […]
  • The 10 Best Resources For
    Laravel The 10 Best Resources For Tips to Contemplate When Finding the Unsurpassed Occupational Therapist When people have issues when it comes to movement, then you ought to contemplate on finding the solution. Occupational therapy can help in improving your movement, which means that you should search for occupational therapy services. However, you can find […]
  • Questions About You Must Know the Answers To
    Laravel Questions About You Must Know the Answers To The Benefits of Minnesota Resorts Vacations are amazing as they allow for one to have a great time where they are away from their day to day life. This means that one gets to be relaxed and not be stressed over work as you are relaxed […]
  • The 10 Best Resources For
    Laravel The 10 Best Resources For Here Are Considerations to Make When Selecting a Pilates Studio A majority of individuals have never heard of Pilates methodology in the past, yet many studios have been offering these techniques for decades. Just recently that many people tend to opt for it as it has proven to provide […]

Creating a MySQL Database for a PHP Application: How to Automate the Creation of MySQL Databases with PHP

by: Mark Alexander Bain

The distribution of a PHP application is always very easy. It is just a matter of compressing the application files, transferring the compressed file to a user’s web server, and then decompressing the files again. In just a few minutes the application can be up and running in a few minutes. However, it’s not quite as easy with the MySQL supporting the application. The MySQL database needs more than a few files to be transferred. Fortunately the programmer can create the database automatically by using some additional PHP code.

The Prerequisites of Working with a MySQL Database and PHP

Before creating the database itself it is worth nothing that PHP comes with its own libraries and objects for working with MySQL (the mysql and mysqli objects and libraries). However, these are not installed by default – this must, therefore, be done before PHP can communicate with MySQL. It’s also worth noting that PHP’s preferred method is currently to use mysqli and not mysql even though both are still available.

Creating a Blank MySQL Database with PHP

Every MySQL database is created with a default administrator user account. This account is called root and is used to:

  • Create new databases
  • Create new user accounts

Obviously the root account will have a password but this cannon be known by the PHP programmer. The PHP application must, therefore, obtain this from the user:

<form method=post>
Enter root password
<input name=root_password type=password>
<input type=submit value=”Create Database” name=create_database>
</form>

Once the password has been obtained then the database can be accessed and the database can be created:

if (isset($_REQUEST[‘root_password’])) {
$user = “root”;
$password = $_REQUEST[‘root_password’];
$host = “localhost”;
$database = “mysql”;
$new_db = “suite101_demo”;
echo “Connecting to the MySQL Database <br>”;
$db = new mysqli ($host, $user, $password, $database);
echo “Creating the new database </br>”;
$db->query(“create database if not exists ” . $new_db);

With the database created the application will also needs to create any tables that will store the data and its own database user.

Creating a MySQL Database User with PHP

The root user account should only be used as a one off – to set up the database in the first place. Any other access by the PHP application should be via a user account set for that purpose, for example:

echo “Creating the application user account<br>”;
$user_sql = “grant select, insert, update, delete
on “. $new_db . “.*
to suite101_user@localhost
identified by ‘suite101_password'”;
$db->query($user_sql);

This new user will be able to view, add, modify and remove data stored in the database tables.

Creating MySQL Database Tables with PHP

The database is now in place as is the database user for the application. The final step is, therefore, to create the tables themselves:

echo “Creating the database tables<br>”;
$db->select_db($new_db);
$table_sql = “create table if not exists articles
(id int auto_increment,
title varchar(255),
published datetime,
primary key (id))”;
$db->query($table_sql);
$db->close();
}
?>

Here the PHP changes to the new database, creates any tables that are required and then closes the connection to the database. This all means that no manual database creation is necessary – it’s all done by the PHP application itself.

The copyright of the article Creating a MySQL Database for a PHP Application in PHP Programming is owned by Mark Alexander Bain. Permission to republish Creating a MySQL Database for a PHP Application in print or online must be granted by the author in writing.


2 Comments

Comments are closed.

RSS Geospatial

  • How to Achieve Maximum Success with Services
    Geospatial How to Achieve Maximum Success with Services The Benefits of Attending a Yoga Retreat A yoga retreat is a perfect option that you may consider when you are planning to go out on a holiday. With a yoga retreat, you get to benefit immensely. You will have an opportunity to relax your body and […]
  • The Path To Finding Better Health
    Geospatial The Path To Finding Better Health The Best Alcohol Rehab Center Any form of addiction, whether it is due to drugs or alcohol, can be treated. There are plenty of ways on how to get the right treatment. One of them is to find the best rehabilitation center. However, your search for the best […]
  • Learning The Secrets About Warranties
    Geospatial Learning The Secrets About Warranties Aspects That You Should Consider When Picking Protection Plan Services If you wish to enjoy the services of gadgets such as smartphones you need to ensure that they are in good shape. This cannot be achieved if they are not well protected from damage. It is your responsibility to […]
  • How I Became An Expert on Resources
    Geospatial How I Became An Expert on Resources Hints on Buying Bathmates Water based pumps are devices for enlarging the male organs. Males consider their organs to be beneficial as part of their body. The male organ has two main functions; reproduction and excretion activity. The waste in the form of urine gets out of […]