Welcome to the world of programming with ridihow.com, we are to treat how to create database, tables and database-connect script with PHP and MYSQL. But before that, what is database-connect?
Database-connect is the script that connects to your database and tables making it possible to store data in the database and retrieve them. We will basically be creating a separate script for the database-connect and using a PHP function called “require_once” or “include_once” to embed the database-connect script to all our pages right! Ok! Let us start.
Assuming you have installed your localhost server basically XAMPP OR WAMP, I prefer you use XAMPP for this purpose. Then, click on your browser and type “localhost/phpmyadmin” to take you directly to a mysql database page where you will be able to create your database and tables.
The structure looks exactly like the one on the image above, then, click “New” you will see an option to create database and host of other databases created either by you.
Key-in any name on the field for “database name” on the field for “collation” kindly leave it on that. Then hit “create” and the database will be created. By looking at the top header you will notice something like this, “Database: testing” it simply means, the database and the name you gave it.
The option for tables that will be under your database and the field for the name of the table because, a database can contain as many as tables.
CREATION OF DATABASE TABLES:
Now, to create a database table look at what you have to do; name the table anything relating to the kind of data you wants it to hold example, “signup”. Pick the number of columns, these columns are the fields from your form example; “name” “email” etc. let us pick 2.
Click “go” and your table will be created under the database you have created that could hold as many as tables you want. Looking above the 3rd image you will see caption like; “Database: testing »Table: testsignup” that is, the database name and the table name.
Coming down you will see the fields to fill according to the number of data you want to store in your database table.
(i):--- In the “name” field key-in:
1. Key-in “name”
2. Key-in “email”
(ii):--- In the “type” field key-in:
The data-type e.g.; charater, varacha, int etc.
1. Name = text: then pick text
2. Email = varachar becaue it contains text, decimals, other abbreviations. Then pick “varacha”
(iii):--- in the “length/Values” field key-in:
The length and value means the number of text or decimals you want that field to contain.
1. Name = “50” no name can exceed 50 letters including spaces.
2. Email = “90” no email exceeds 90 character including spaces.
Then click “save” and your table fields have been created accordingly. But, did you discover one thing we omitted here? The table has to be indexed, that is, every submitted data have to bear a particular auto-incrementing identity to identify such data and increment when any other data is submitted.
So, to include it in our table field we have to do one thing, click on “drop down menu before GO and pick, AT THE BEGINNING OF THE TABLE” to add one more column. Click “go” at “NAME” fill-in = “id” “TYPE” “= tinyint” and “LENGTH/VALUES” = “11” then the most important thing is the auto-increment section structure which is represented with “A_I” just fill the check box and hit “save”
The table is now index with a primary key (index key) that identify every data submitted to your table with your form. Keeping close watch at the image 4 you will see a key-like structure near the id, which is an indication that the table is indexed.
HOW TO MAKE THE TABLE HOLD DATA:
Now the question remains, how can we make the table to hold data? That is where the database-connect script of PHP code comes in.
CREATING THE DATABASE-CONNECT SCRIPT;
Open up your dream weaver adobe and click new to go to the code area. Open with the php opening tag which is
$mysqli_hostname = “localhost”;
$mysqli_user = “root”;
$mysqli_password = “”;
$mysqli_database = “testsignup”;
$db = mysqli_connect($mysqli_hostname, $mysqli_user, $mysqli_password, $mysqli_database) or die("Could not connect database");
mysqli_select_db($db, $mysqli_database) or die("Could not select database");
$db = mysqli_connect($mysqli_hostname,$mysqli_user,$mysqli_password,$mysqli_database);
This is the script we will be embedding on our form script to enable database connection and submission of our data into our database table. Let us examine them line by line.
1. Php = this is the PHP opening tag.
2. $mysqli_hostname = “localhost”; = the server that is hosting your script which is “localhost” because it is not on the internet.
3. $mysqli_user = “root”; = the user name of the database and always leave it on “root” for simulation.
4. $mysqli_password = “”; = the password used in pass-wording you server. “” means it is open and no password.
5. $mysqli_database = “testsignup”; = the database name.
6. $db = mysqli_connect($mysqli_hostname, $mysqli_user, $mysqli_password, $mysqli_database) or die("Could not connect database"); = simply means “$db” is the database, “mysqli_connect” is connect to the hostname, with password, and a particular database which you have created already.
Or die “could not connect database” this is the error message you will get after running the script and it did not connect to the database.
7. mysqli_select_db($db, $mysqli_database) or die("Could not select database"); = then select the database or error if it could not select.
8. ?> = close the php tag opened earlier.
Then, this is what we will embed in our form script to enable us connect to database and submit. How can we do that? This is just simple and it is as below:
On the form script, just at the header after, HTML TAG OPENING
This will embed the script dbc.php automatically in every script without repeating the code again and again.
Thank you for reading my article at ridihow, am a web programmer and we will continue next time with the PHP code that will enable you submit the data in our database table.
The Images & Article Headers re-direct To Similar Reference Content(Url)