Beginners PHP - Creating Databases and Tables
2 posts
Page 1 of 1
Hey guys,
This PHP tutorial is part of a series from me and Benji_19994. We are covering creating databases, tables and connecting to a database with MySQLi.
First step is to make sure you webserver is running, you learnt how to do this in the Beginners PHP - Xampp tutorial.
Create a new file called conn_db.php, we will use this file to store our database information.
We need to create our database. We can do this with PHP with a SQL query or we can do it on PhpMyAdmin panel.
In our new file conn_db.php we need the following code:
Keep that file saved, we will come back to it in a minute.
Creating the database:
Navigate to 'localhost/phpmyadmin' in your browser. Click the databases button.

The panel can look overwhelming at once, but you will get used to it after awhile. Clicking 'Databases' comes to this screen:

From here enter the name of our new database 'codenstuffdb' then click create. We should now see 'codenstuffdb' in the list of databases.
The phpmyadmin tabs:
Databases:
- Used to create new databses
SQL:
- Run SQL Queries for your databases/tables
Export:
- Use this from one of the databases selected or click the "Server 127.0.0.1" button and choose what you want to export, you will be given a .sql file with all the databases, tables and data which you can then use to import or run as queries later on.
Import:
- Import a .sql file
That's not all the tabs but they are the main ones we use.
So far we have created a new database called 'codenstuffdb' and we created a PHP file to connect to our database. There are a few ways we can create a table for our database.
We could click the database and an option to create a table will appear, we could run a SQL query from the admin panel or we can do it from PHP. Let's use the panel to create the table first.
Click on 'codenstuffdb' from the list of databases on the left then click the SQL tab. Here is our query:
If you run the SQL query you should have the table in your new database.
Using this:
Next tutorial I will cover retrieving data from a database and displaying it on the page, and inserting data. Tutorial after that will be on updating and deleting.
This PHP tutorial is part of a series from me and Benji_19994. We are covering creating databases, tables and connecting to a database with MySQLi.
First step is to make sure you webserver is running, you learnt how to do this in the Beginners PHP - Xampp tutorial.
Create a new file called conn_db.php, we will use this file to store our database information.
We need to create our database. We can do this with PHP with a SQL query or we can do it on PhpMyAdmin panel.
In our new file conn_db.php we need the following code:
Code: Select all
We have 4 string variables, the host name, username, password and database name. With PhPMyAdmin the defaults are locahost, root and no password. The database name will depend on what name you give your database. <?php
//Connection Information
$server = "localhost";
$user = "root";
$password = "";
$database = "codenstuffdb";
//Connect to the server
$mysql_link = mysqli_connect($server, $user, $password, $database)
or die("Error " . mysqli_error($mysql_link));
?>
Keep that file saved, we will come back to it in a minute.
Creating the database:
Navigate to 'localhost/phpmyadmin' in your browser. Click the databases button.

The panel can look overwhelming at once, but you will get used to it after awhile. Clicking 'Databases' comes to this screen:

From here enter the name of our new database 'codenstuffdb' then click create. We should now see 'codenstuffdb' in the list of databases.
The phpmyadmin tabs:
Databases:
- Used to create new databses
SQL:
- Run SQL Queries for your databases/tables
Export:
- Use this from one of the databases selected or click the "Server 127.0.0.1" button and choose what you want to export, you will be given a .sql file with all the databases, tables and data which you can then use to import or run as queries later on.
Import:
- Import a .sql file
That's not all the tabs but they are the main ones we use.
So far we have created a new database called 'codenstuffdb' and we created a PHP file to connect to our database. There are a few ways we can create a table for our database.
We could click the database and an option to create a table will appear, we could run a SQL query from the admin panel or we can do it from PHP. Let's use the panel to create the table first.
Click on 'codenstuffdb' from the list of databases on the left then click the SQL tab. Here is our query:
Code: Select all
In this create we create a table called languages if it doesn't exist. We create a column named ID that has a data type of int that has a length of 10 characters, NOT NULL means it needs to have a value in the rows and it will auto increment. We create another column called languages and has a datatype of varchar (text) and has a length of 50 characters, it cannot have blank rows and then we set the id column as a primary key. CREATE TABLE IF NOT EXISTS languages
(
id int(10) NOT NULL AUTO_INCREMENT,
languages varchar(50) NOT NULL DEFAULT '',
PRIMARY KEY (id)
);
If you run the SQL query you should have the table in your new database.
Using this:
Code: Select all
You should be able to connect to the database without any errors. <?php
require_once("conn_db.php");
?>
Next tutorial I will cover retrieving data from a database and displaying it on the page, and inserting data. Tutorial after that will be on updating and deleting.
My name is Tom | Visit my blog where I post new content every day! Tom's Daily Blog | MineCraft is awesome!
Never display the actual error returned by the database to the website administrator or a visitor.
Code: Select all
or die("Error " . mysqli_error($mysql_link));
You can find me on Facebook or on Skype mihai_92b
2 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023