Connectivity of MySQL Database with PHP in localhost with Login authentication
Connectivity of MySQL Database with PHP in localhost with the example of Login authentication:-
For connecting the MySQL database with PHP the required softwares are:-
1)PHP
2)MySQL
3)Apache or any HTTP Server
Please download these softwares from their official website then install in your computer after installation the configuration is required for making a working localhost
Or
Download the preconfigured WAMP Server from the official website and save your time.
Now after the installation of the WAMP server you need to create the MySQL username and password, For creation of MySQL username and password the following SQL command is used in the prompt (please check below the snapshots for opening the prompt and submitting the command):-
mysql> create user ‘mysql_username‘@’localhost’ identified by ‘mysql_password‘;
After the creation of MySQL username and password you need to grant all the privileges for the user you created, for this the following query is used:-
mysql> grant all privileges on *.* to ‘mysql_username‘@’localhost’;
For creation of the database we also use the MySQL prompt with the following query:-
mysql> create database myfirstdb;
After the creation of MySQL username, password and database we creation the table for our database, for this we create the following PHP code:-
CODE(create_table_using_php.php):-
<?php $host_name="localhost"; // Host name $my_user_name="mysql_username"; // Mysql username $my_password="mysql_password"; // Mysql password $my_first_db="myfirstdb"; // Database name // Connect to server and select databse. $con=mysqli_connect("$host_name", "$my_user_name", "$my_password", "$my_first_db"; if (!$con) { die('Could not connect: ' . mysql_error()); } // For Creation of table $sql="CREATE TABLE myFirstTable(username CHAR(45),password CHAR(45))"; if (mysqli_query($con,$sql)) { echo "Table myFirstTable created successfully!!"; } else { echo "Error in the creation of table: " . mysqli_error(); } ?>
(These shows that we successfully connected our MySQL Database with PHP.)
After successfully creation of table we move to our PHP Script of Login authentication for the verification of database connectivity(i.e. the connectivity of MySQL database with PHP), for this I am going to create three .php file those are below:-
CODE(login_form.php):-
<html> <head> <title>Login System</title> </head> <body> <form action="check_the_user.php" method="post"> Username: <input type="text" name="myusername"></br> Password: <input type="password" name="mypassword"></br> <input type="submit" value="Submit"> </form> </body> <html>
CODE(dbinfo.php):-
<?php $host_name="localhost"; // Host name $my_user_name="mysql_username"; // Mysql username $my_password="mysql_password"; // Mysql password $my_first_db="myfirstdb"; // Database name $tbl_name="myfirsttable"; // Table name ?>
CODE(check_the_user.php):
<?php import("dbinfo.php"); // Connect to server and select databse. $con=mysql_connect("$host_name", "$my_user_name", "$my_password"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("$my_first_db")or die("can't select Data Base"); // username and password sent from form the html page using the POST method $usrname=$_POST['myusername']; $pwd=$_POST['mypassword']; // To protect MySQL injection $usrname = stripslashes($usrname); $pwd = stripslashes($pwd); $usrname = mysql_real_escape_string($usrname); $pwd = mysql_real_escape_string($pwd); $sql="SELECT * FROM $tbl_name WHERE username='$usrname' and password='$pwd'"; $result=mysql_query($sql); // Mysql_num_row is counting table row $count=mysql_num_rows($result); // If result matched $usrname and $pwd, table row must be 1 row if($count==1){ echo "AUTHENTIC USER"; } else { echo "UNAUTHENTIC PASSWORD or USERNAME"; } mysql_close($con); ?>
If the output shows
AUTHENTIC USER
that means the login in authentic, otherwise the input details is wrong.
This login can be customize by editing the if condition in check_the_user.php file according to your requirement.
« CBSE Class 11th Physics Sample Paper Vintage Style And Fashion »
Tell us Your Queries, Suggestions and Feedback