In the previous MySQL tutorial, we saw how to select a
database at the time of establishing a connection or after the connection is established by using 'mysqli_select_db()'
function. But what if we don't have a database and we don't want to go to the 'phpmyadmin'
dashboard to create a MySQL database. In this case, we can use PHP's 'mysql_query()'
function to create a MySQL
database right from a PHP website's dashboard.
A Database is a collection of inter-related data stored under one name. It is used to efficiently store, retrieve and delete data whenever needed. In MySQL, the database is a Relational DBMS (RDBMS) which stores the data in the form of tables. Infinite tables can be created inside a database and can be managed with ease in MySQL DBMS. For example, for a school's database, the tables can have data about students, teachers, staff, etc.
In SQL, any kind of operation in the database is implemented through a query. So, for every task, we should know the query to execute it. This query will be passed as a parameter to the 'mysqli_query()'
function and then it will get executed.
Remember that the first and foremost requirement is to establish a connection with a database connection. Only after that, any function related to the MySQL database will work.
Let's see the query to create a database in SQL. It is very simple and as SQL is a High Level language it is easy to understand also. We're assuming that you already have an SQL Server available to use, if not, please check out the "getting started guide".
The SQL "CREATE DATABASE"
statement is used to create a database. Let's see the full syntax:
CREATE DATABASE database_name;
This SQL statement creates a database named mydb
:
CREATE DATABASE mydb;
Just remember that only creating a database is not sufficient you have to select the database, create tables inside it, in order to insert, retrieve and delete data from it. We will see all these operations but right now we will just focus on how to create a database in MySQL.
To execute a query using the procedural approach we will use 'mysqli_query()'
function. We will pass the SQL's 'CREATE DATABASE' command in the 'mysqli_query()' function along with the connection variable you have set(Most Important) as the first parameter. Remember that, you can directly pass the query as a 2nd parameter to the function or you can first store it as a string in a PHP variable and then can pass that variable as the second parameter.
The mysql create database examples
given below will clarify things for you.
To execute a query using the MySQli object oriented approach, we will use the 'query()'
function of MySQli class.Obviously we will access this function using the connection object. This function only has one parameter in which we can pass the query directly or also pass a string variable containing the query. TThe mysql create database examples
given below will clarify things for you.
In the PDO extension approach although making a connection is a bit complicated as compared to MySQLi or MySQL
extension but executing a query is quite simple. The 'exec()'
function is used in PDO approach to execute the queries. Same as the previous two extensions, we will pass the query as a parameter to 'exec()' function. Look at the example below to see how you can use this function.
In the example above we have set the PDO::ATTR_ERRMODE attribute to PDO::ERRMODE_EXCEPTION, it tells PDO to throw exceptions whenever a database error occurs. The exception handling feature is a great benefit of PDO.
If in case, any exception is thrown within the 'try{ }'
block, the script stops executing and flows directly
to the first 'catch(){ }'
block. In the example above, the catch block echoes the SQL Query and then
generates an error message.
Follow Us: