Creating
MySQL database is very easy. However, before starting the project, you need to
have an admin privilege. In PHP, you can use mysql_query function when creating
a MySQL database. This article will explore more about MySQL database and how
to create a MySQL database using MySQLi and PDO. MySQL database has been used
widely in many applications and websites including WordPress, Joomla, TYPO3,
Facebook, Google, etc. It is originally created by MySQL AB, a Swedish company,
and its first version appeared in 1995. Now, My SQL uses the MySQL Server
8.0.0-dmr that has been launched on 12 September 2016.
Dual Procedural and Object Oriented
The
extension on MySQLi offers a dual interface. They consist of the object
oriented and procedural programming paradigm. When you use the PHP MySQLi
object oriented example, the documentations will be arranged using object
oriented interface. The functions are shown according to their groups. Make it
simpler to get started. On the other hands, the procedural interface provides
the similar interface as the previous MySQL extension. Some old users of MySQL
may prefer this interface. Mostly, the function names on this interface are
only differentiated by prefix. Actually, both interfaces do not have
differences in terms of performance. The users can choose freely based on their
preference.
PDO on MySQL
The MySQL
functions have been used since a long time. However, it is no longer support
some modern SQL concepts like stored procedures, prepared statements,
transactions, etc. That is why new programmers prefer to use PDO. PDO has a
better interface. It makes the users able to write a cleaner and safer code. In
creating an example PDO MySQL you need to take four parameters.
·
DSN/ a string of options
which tell PDO to use the driver
·
Username
·
Password
·
An array of driver options
An error
sometimes occurs in the character sets. It happens if you forget to write the
charset parameter. Charset is very important for the security, so make sure you
include it to your program.
Creating MySQL database
To create
the MySQL database, you can start by writing ‘mysql> CREATE DATABASE
menagerie;’. The code is case sensitive so you always have to write ‘menagerie’
not ‘Menagerie’ or ‘MENAGERIE’. You only need to create the database once.
However, you need to select it first whenever you want to start a MySQL
session. You can use a USE statement each time or select one in the command
line for example ‘mysql> USE menagerie Database changed’. If you encounter
errors such as ERROR 1044, it means that the user account does not have the
privileges yet.
In adding
tables to MySQL database, the steps are almost the same as creating a database.
First, you need to create an SQL query for the table and then execute it using
mysql_query() function. When you need many tables, it is recommended to start
with creating a text file and then write all the commands in that file. Next,
you can load the text file into $sql variable and then execute the commands.
Nice article !
BalasHapus