Useful Tips

PHP5 MySQL Create Database

Pin
Send
Share
Send
Send


The creation of modern electronic computers has allowed us to automate data processing in many areas of human activity. Currently, without modern data processing systems, it is difficult to imagine advanced production technologies, economic management at all its levels, scientific research, education, publishing, the functioning of the media and much more. Such data processing systems are called information systems.

Currently, information systems based on database technologies, called the "database management system" (DBMS), are widely used. Born about four decades ago, this technology has proven to be very fruitful. The modern DBMS is based on a relational data model, for interacting with a relational database, in the mid-70s, IBM developed a structured SQL query language, which later became the standard when working with databases.

In our information time, there are many programs for working with databases, such as: InterBase, FilePro, mSQL, MS Access, Oracle, MySQL, etc.

We’ll look at how you can create and work with a database locally, using the capabilities of PHP and MySQL, for this we need a “gentleman's set” of Denwer web developer.

Denwer is a local server that already has: Apache, PHP, MySQL, Perl and more. It is also easy to install, which is important at the first stage of working with web-programming, creating the first base in PHP.

We will discuss with you how to create a database using the example of a single table and work with it, using PHP and HTML forms.

To begin with, we need to determine the theme of the database, in which we need to highlight the entities and their characteristics, which our entity (s / s) will have. Then we decide on the types of data - characteristics that the entity has. And the last stage of the preparatory phase is the determination of the relationships between our entities (“one to one”, “one to many”, “many to many”).

For example, we will create a small base “Computer technology (komp_texn) » with one table "Product (product). "

product / product

product_ID / primary key

Once you are done with these steps, we proceed to the direct creation of a relational database in local access - phpMyAdmin.

From the beginning, you need to start Denwer, it is assumed that you have already installed it by double-clicking on the shortcut on the desktop "StartDenwer ». After that, launch any browser, and in the address bar, enter http: // localhost, and in this page, go to http: // localhost / Tools / phpMyAdmin - "VerificationMySQL andphpMyAdmin ».

In the page that appears, in the top menu, select the “Database", enter the name of the created database - “Komp_texnika"(See Fig. 1).

Then we proceed to the creation of the entity / tables, the database, describing its name and how many characteristics you select, for this entity - «Product ”, 4 - column (see Fig. 2).

The next step will be a description of the characteristics / properties, and an indication of their types in the respective ranges (see Fig. 3).

Now our database has an empty table Productthat you want to fill out, for this, in the upper menu, select the tab “Embed»And you will see a page for filling out these fields (see Fig. 4).

I want to draw your attention to the fact that only two entries can be inserted into the table from the beginning. To add more values, select the item at the bottom of the page in the drop-down list "Add new entry" and click OK.

Filling the database with information (see Fig. 5), we will work with the database using the PHP script.

First, create an HTML form to fill out the information in our table, that is, create a direct link between the table in the database and the form (see table 1.).

Now we reflect the available information in the database on the web page, in the form of an array of data and adding some features in working with data. That is, adding, editing and deleting data. To implement these actions, you need to create three more web pages for: creating new data - new.php (see table 2.), editing data - edit.php (see table 3.) and deleting data from the table in the database - delete.php (see table 4.).

By combining all these actions into one web page called index2.php, we can freely work with the database in the usual form that is understandable to the user (see table 5.).

This is all that I wanted to consider creating a database in PHP, after we’ll look at how to work with several tables in a database using SQL queries.

Bibliography

  1. Ostrovsky, S.L. Lecture 6. Own magazines closer to the body [Text] / S.L. Ostrovsky // Computer Science. - 2008.-№22.-p.2-13.
  2. Yakovleva, Tamara What is Denwer (Denver) and how to install it? [Electronic resource] / T. Yakovleva //URL: http://blog.sistemainternetzarabotka.ru/chto-takoe-denwerdenver-i-kak-ego-ustanovit.
  3. Moiseenko Sergey Interactive textbook on SQL [Electronic resource] / S.Moiseevnko // URL: http://www.sql-tutorial.ru/.
  4. PHP and MySQL. Joint work [Electronic resource] // URL: http://www.softtime.ru/bookphp/gl12_1.php.

The electronic periodical is registered with the Federal Service for Supervision of Telecommunications, Information Technologies and Mass Communications (Roskomnadzor), certificate of registration of mass media - EL No. FS77-41429 of July 23, 2010

Media co-founders: Dolganov A.A., Mayorov E.V.

MySQLi Example - Object Oriented

connect_error) <
die ("Connection error:". $ conn-> connect_error),
>


// Create Database
$ sql = "CREATE DATABASE myDB",
if ($ conn-> query ($ sql) === TRUE) <
echo "Database created successfully",
> else <
echo "Error creating database:". $ conn-> error,
>

// Close connection
$ conn-> close (),
?>

Note: When creating a new database, you should specify only the first three arguments to the infrastructure object (server, username and password).

PDO example

// Create Database
$ sql = "CREATE DATABASE myDBPDO",

// Use exec () as the result is not returned
$ conn-> exec ($ sql),
echo "Database created successfully
",
>
catch (PDOException $ e) // exception class
<
echo $ sql. "
". $ e-> getMessage (),
>
>

// Close connection
$ conn = null,
?>

Tip: The big advantage of PDO is that it has an exception class to solve any problems that may arise in our database queries. If an exception is thrown inside the try <> block, the script stops execution and jumps directly to the first catch () <> block. In the catch block below, the echo $ sql statement generated an error message.

Pin
Send
Share
Send
Send