SQL is a server-side scripting language used to query relational databases such as MySQL database. Relational databases as the name referrers are ways in which data is stored in formatted clusters such as databases and their corresponding SQL server tables in ways and means which creates relationships between tables of similar or even different databases. The language is used to manipulate data stored on the server by creating and deleting tables, creating and dropping databases, inserting and updating data and, creating databases. All of this can be achieved programmatically through various server-side languages such as Python and PHP but in this example, we shall be using PHP to demonstrate how to build a SQL server database.
To begin, follow these steps:
They are other text editors like Dreamweaver but let us use notepad++ for now. Notepad can be downloaded and used for free.
This file will be responsible for creating a connection to our MySQL database. Write the following and save the file as db.php inside our query folder which we created.
// Php opener tag.
<?php
//During the installation of XAMP it prompted for a username and password to create a user with privileges over the server if left unchanged the values are root and empty space ” “respectively. The server name should be left as a localhost.
$servername = "localhost";
$username = "username";
$password = "password";
// Code to create a connection
$conn = mysqli_connect($servername, $username, $password);
// Code to confirm connection
if (!$conn) {
// Check the connection and display any error that might be preventing the PHP code from creating a successful connection.
die("Connection failed: " . mysqli_connect_error());
}
// Php closing tag
?>
After creating the db.php we can create another file to execute and made a database using SQL. The following code does just that.
<?php
// Php code to include the created db.php and hence inherit connection created.
include("db.php");
// SQL code to create a database
$sql = "CREATE DATABASE myDB";
// Code to execute SQL query
if (mysqli_query($conn, $sql)) {
echo "Database created successfully";
} else {
echo "Error creating database: " . mysqli_error($conn);
}
/
mysqli_close($conn);
?>
The above code can be used to create a SQL database called myDB just save the file as index.php, inside the query folder we created in the XAMP htdocs folder, and run it by going to the browser and going to localhost (localhost represents the htdocs XAMP htdocs folder and hence can be used to access and navigate to files and other folders inside it) then query (by default the server will always execute a file with the name index inside the folder it navigates inside and as such it is not a prerequisite to prefix the folder name with the index.php file require.) The complete URL should look like http://localhost/query or http://localhost/query/index.php both would give a similar result.
The above code can be modified to use a variable as an argument in building a database using SQL with the complete code looking like:
<?php
// Php code to include the created db.php and hence inherit connection created.
include("db.php");
.$mdatabase="hellowworld";
// Sql code to create database
$sql = "CREATE DATABASE".$mdatabase;
// Code to execute Sql query
if (mysqli_query($conn, $sql)) {
echo "Database created successfully";
} else {
echo "Error creating database: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
Another implementation would involve creating a PHP function to execute a query to create a database using overloaded data. Such an implementation would like:
<?php
// Php code to include the created db.php and hence inherit connection created.
include("db.php");
function create($conn, $datab)
{
.$mdatabase=$datab
// SQL code to create a database
$sql = "CREATE DATABASE".$mdatabase;
// Code to execute SQL query
if (mysqli_query($conn, $sql)) {
echo "Database created successfully";
} else {
echo "Error creating database:
” . mysqli_error($conn);
}
}
mysqli_close($conn);
?>
This way someone could easily call create (// The connection $conn,// The name of the database to be created “mydb”);
It is important to notice that SQL is language on its own independent from the server-side language running it. This means that a SQL query to create a database implemented in PHP would be similar to one executed by python or C# using MySQL connector to connect to the server. It would also be good to state that all the sample scripts would create empty databases which would have to be populated with tables and later columns and rows for it to be fully functional, also a GUI to modify and monitor the databases, and their corresponding data, can be accessed from http://localhost/phpmyadmin