Create Mysql Database and Table Using PHP in Xampp: Prerequisites
Create Mysql Database and Table Using PHP in Xampp: Prerequisites
Create Mysql Database and Table Using PHP in Xampp: Prerequisites
Open Source
Technology
Linux
Unix
Free Ebooks And Videos
Donate
Contact Us
Home
Backup tools
Command line utilities
Database
DevOps
Mobile
Programming
Security
Virtualization
Home PHP MySQL Create MySQL Database And Table Using PHP In XAMPP
PHP MySQLDatabaseLAMP StackLEMP
StackLinuxMariaDBMySQLPHPProgrammingXAMPP
In this guide, we will discuss how to create a MySQL database and table using PHP and also
how to create the MySQL/MariaDB databases and tables via phpMyAdmin in XAMPP stack.
Prerequisites
Make sure you have setup XAMPP stack in your system. The following guide explains how to
setup XAMPP stack in Linux.
Setting up XAMPP is much easier than LAMP and LEMP stacks. So, we will will be using
XAMPP stack throughout this guide.
Here, my servername is localhost, MySQL username is root and its password is empty.
Code:
Now, let us write a sample PHP code based on the above steps.
Create a new PHP file named Connect_data.php under the /htdocs directory with the
following contents in it.
Heads Up: If you use Linux, the htdocs folder will be under /opt/lampp/ directory. If you're
on Windows, the htdocs will be usually in C:\xampp\ folder.
<?php
//specify the server name and here it is localhost
$server_name = "localhost";
Heads Up: If you're using LAMP/LEMP stacks, you may need to update the MySQL root
password in the above code.
Open your web browser and point it to http://localhost/Connect_data.php URL. If you see the
"Connection established successfully" message, you're connected with XAMPP.
1. Start the Apache and MySQL modules from XAMPP control center.
3. Click the Databases tab and enter the Database name that we are going to create. Here, I use
"My_Company" as the database name. Leave the second tab as it is and click on Create.
Create Database Via phpMyAdmin
A new database named "My_Company" has just been created. You can view the newly created
database from the left pane or on the top title bar of the phpMyAdmin dashboard.
View Database In PhpMyAdmin
We can also do the same withe a piece of PHP code as described in the following section.
Query Syntax:
Steps
The first three steps are same as we did while establishing connection with MySQL in the earlier
step.
1. Specify servername, mysql username and password fields in the PHP code.
Code:
In this step, we specify the SQL query to create the database and store it in a variable.
Remember, we've already created a database named "My_Company". So, we will now create a
new database called "mycompany" and we are storing it in a variable named query.
Code:
If we want to check if the database is created or not, we can use the mysqli_query() function.
Code:
mysqli_query($connection, $query)
If this is True, then a database is created. Otherwise it will return an error. We can check the
error by using the mysqli_error($connection) function.
This is the last step where we have to close the connection by using the mysqli_close()
function.
Code:
mysqli_close($connection);
Now let us write the actual PHP code to accomplish the above steps.
Please note that we are going to create a new MySQL database named "mycompany" in this
example.
Create a new PHP file named create_database.php under the /htdocs directory with the
following contents in it.
<?php
//specify the server name and here it is localhost
$server_name = "localhost";
mysqli_close($connection);
?>
Open your web browser and navigate to http://localhost/create_database.php. You will see an
output like this in your browser window.
You can verify if the database "mycompany" is actually created from the phpMyAdmin
dashboard.
View Databases In PhpMyAdmin
2. Select the database in which you want to create tables. Here, I will be using "mycompany"
database.
Provide the table Name and number of columns to be present in the database. Here, I give the
table name as SALES and number of columns are 3. Click Go to create the table.
Create Table Manually Via phpMyAdmin
3. Specify the column names and length of column with data type.
Here, INT represents Integer values and VARCHAR represents string values.
After entering the column names and their length, click Save button at the bottom.
Enter Column Name And Length
Once the columns are created, you will see them under under "Structure" tab.
View Table Structure
In this way, you can create as many tables as you want via phpMyAdmin.
Now, we will see how to create a table using a PHP code snippet.
Code:
Where, table_name is the name of the table and Column_name specifies the name of the column
along with its datatype.
Steps
Creating tables in a MySQL database using PHP code consists of the following steps.
1. Specify MySQL server name, username, password and database name in the PHP code.
We already have created a table called "Sales" under "mycompany" database. So, we will now
create a new table called "Service" in the same database.
Code:
In this step, we specify the SQL query to create a table called "Service" in the "My_Company
database" and store it in a variable named query. The table "Service" has three columns.
Code:
To check if the table is created or not, we can use the mysqli_query() function.
Code:
mysqli_query($connection, $query)
If this condition is True, then a table will be created. Otherwise it will return an error. We can
check the error by using the mysqli_error($connection) function.
This is the last step where we have to close the connection by using the mysqli_close()
function.
Code:
mysqli_close($connection);
Now let us write a sample PHP code based on the above steps.
Create a new PHP file named create_table.php under /htdocs directory with the following
contents in it.
<?php
//specify the server name and here it is localhost
$server_name = "localhost";
Now let us write a sample PHP code based on the above steps.
Open your web browser and navigate to http://localhost/create_table.php. You will see an
output like this in your browser window.
A Table Is Created In A Database
You can verify if the table called "Service" is actually created in the "My_Company" database
via phpMyAdmin dashboard.
Expand the database name (i.e. My_Company) on the left pane and choose the table name (i.e.
Service). And then click Structure tab on the right pane. Under the Structure tab, you will see the
table's columns.
View Table Structure In PhpMyAdmin
Conclusion
In this step by step tutorial, we discussed how to connect with MySQL database and how to
create a MySQL database and a table using PHP code and via phpMyAdmin dashboard.
We have provided sample PHP codes for your reference. Amend the code as per your
requirement and check it in XAMPP. In our upcoming articles, we will learn more PHP MySQL
related topics.
Read Next:
CodeDatabaseLinuxmacOSMariaDBMySQLMySQL CommandsPHPPHP
MySQLphpMyAdminProgrammingWindowsXAMPP
0 comment 8
Sravan Kumar
Previous post
Next post
Leave a Comment
Save my name, email, and website in this browser for the next time I comment.
* By using this form you agree with the storage and handling of your data by this website.
This site uses Akismet to reduce spam. Learn how your comment data is processed.
Newsletter
Subscribe our Newsletter for new posts. Stay updated from your inbox!
In this tutorial, we are going to learn what is XAMPP stack and how to install XAMPP in Linux
operating systems. Next, we willl discuss how to start or restart XAMPP server and how to
access XAMPP test page, phpMyAdmin dashboard. Finally, we will see how to secure XAMPP
installation and remove XAMPP if it is no longer required.
1.
2.
1.
2.
3.
3.
1.
2.
5.
1.
6.
7.
8.
9.
The XAMPP is widely used by developers to test their web applications in their local system
before uploading them to the production system.
XAMPP is short for Cross-platform (X), Apache Web server (A), MariaDB (M), PHP (P), and
Perl (P).
Go to Apache Friends website and download the latest available version. As of writing this
guide, the latest version was 8.1.4.
Download XAMPP For Linux
$ cd Downloads
$ chmod +x xampp-linux-x64-8.1.4-1-installer.run
Or,
$ sudo ./xampp-linux-x64-8.1.4-1-installer.run
You will prompted to answer a couple questions. Simply type "Y" to all questions and complete
the installation.
----------------------------------------------------------------------------
Welcome to the XAMPP Setup Wizard.
----------------------------------------------------------------------------
Select the components you want to install; clear the components you do not
want
to install. Click Next when you are ready to continue.
----------------------------------------------------------------------------
Installation Directory
----------------------------------------------------------------------------
Setup is now ready to begin installing XAMPP on your computer.
----------------------------------------------------------------------------
Please wait while Setup installs XAMPP on your computer.
Installing
0% ______________ 50% ______________ 100%
#########################################
----------------------------------------------------------------------------
Setup has finished installing XAMPP on your computer.
$ sudo ./xampp-linux-x64-8.1.4-1-installer.run
The XAMPP installer wizard will open now. Click Next to continue.
XAMPP Setup Wizard
Select the XAMPP components you want to install and click Next.
Select XAMPP Components To Install
Now, the installer will display the default installation path of XAMPP. By default, XAMPP will
be installed in /opt/lampp directory. Click Next to continue.
XAMPP Installation Path
XAMPP installation is completed now. if the "Launch XAMPP" box is checked, the XAMPP
control panel will start automatically.
XAMPP Installation Is Completed
Please note that you will have to manually start XAMPP at every system reboot by running the
following command:
You will now be greeted with XAMPP control panel welcome screen.
XAMPP Welcome Screen
You can start the XAMPP control panel at any time by running the following command:
$ sudo /opt/lampp/manager-linux-x64.run
As you can see in the above screenshot, the welcome screen shows the following 4 tabs. Clicking
on each tab will get you to the respective section.
This section shows the list of modules that are running or stopped.
As you can see in the above outpout, the netstat command is not available. Netstat is part of the
"net-tools" package. To fix this, simply install net-tools package.
The net-tools package is available in the default repositories of most Linux distributions. For
example, you can install net-tools on Debian-based system using the following command:
Sample output"
Sample Output:
Congratulations! We have successfully setup XAMPP stack in our Linux system. You can now
start testing the web applications!
To view the PHP information, simply click PHPInfo link on the top from the XAMPP test page.
Alternatively, you can directly navigate to http://localhost.phpinfo.php from your web browser.
PHP Info Page
5. Access PhpMyAdmin
To access phpMyAdmin dashboard, click the phpMyAdmin link from the XAMPP test page or
directly navigate to http://localhost/phpmyadmin from the browser's address bar.
Access PhpMyAdmin
There is no password for phpMyAdmin. If you want to secure phpMyAdmin admin account,
refer the "Secure XAMPP" section below.
By default, phpMyAdmin can only be accessed from the localhost itself. If you want to access it
from a remote system on the network, edit /opt/lampp/etc/extra/httpd-xampp.conf file:
<Directory "/opt/lampp/phpmyadmin">
AllowOverride AuthConfig Limit
Require local
ErrorDocument 403 /error/XAMPP_FORBIDDEN.html.var
</Directory>
<Directory "/opt/lampp/phpmyadmin">
AllowOverride AuthConfig Limit
Require all granted
ErrorDocument 403 /error/XAMPP_FORBIDDEN.html.var
</Directory>
Edit httpd-xampp File
Save the file and close it. Restart XAMPP service to take effect the changes.
You can now access the phpMyAdmin dashboard from any remote system on your local area
network by navigating to http://IP-Address/phpmyadmin URL.
Access PhpMyAdmin From Remote Systems
It has certain configuration settings that make it easy to develop locally but that are insecure if
you want to have your installation accessible to others.
If you want have your XAMPP accessible from the internet, make sure you understand the
implications and learn how to protect your site.
Alternatively, you can use LAMP or LEMP stacks which are similar packages which are more
suitable for production.
You will be prompted to answer a couple questions to secure XAMPP. Answer "Yes" to all the
questions and set the password for MySQL root user, PhpMyAdmin admin user, and ProFTPD
admin user.
At this stage, you should have a local, secure web development environment with XAMPP.
8. Uninstall XAMPP
Go to the location where XAMPP is installed:
$ cd /opt/lampp/
And, run the following command to remove XAMPP stack from your system:
$ sudo ./uninstall
You will be prompted if you want to remove XAMPP including all the modules. Type "Y" and
hit enter to uninstall XAMPP.
----------------------------------------------------------------------------
Uninstall Status
Uninstalling XAMPP
0% ______________ 50% ______________ 100%
#########################################
Conclusion
In this guide, we discussed what is XAMPP, and how to install XAMPP in Linux operating
systems. We also looked at how to start or restart XAMPP modules and how to access XAMPP
test page, php info page, and phpMyAdmin dashboard. Finally, we saw how to secure XAMPP
installation and then how to remove the XAMPP stack from a Linux system.
Senthilkumar Palani (aka SK) is the Founder and Editor in chief of OSTechNix. He is a
Linux/Unix enthusiast and FOSS supporter. He lives in Tamilnadu, India.
Previous post
Next post
Leave a Comment
Save my name, email, and website in this browser for the next time I comment.
* By using this form you agree with the storage and handling of your data by this website.
This site uses Akismet to reduce spam. Learn how your comment data is processed.
Newsletter
Subscribe our Newsletter for new posts. Stay updated from your inbox!
About OSTechNix
OSTechNix (Open Source, Technology, Nix*) regularly publishes the latest news, how-to
articles, tutorials and tips & tricks about free and opensource software and technology.
Archives
Popular Posts
1
August 6, 2020
2
3
August 9, 2018
About
Contact Us
Privacy Policy
Sitemap
Terms and Conditions
OSTechNix © 2022. All Rights Reserved. This site is licensed under CC BY-NC 4.0.
This website uses cookies to improve your experience. By using this site, we will assume that
you're OK with it. Accept Read More
1.
2.
1.
1.
2.
6.
1.
2.
7.