You are on page 1of 7

BCA III SEM & M.

Sc(CS) IV Sem
PHP Unit V: Module No. 1
MYSQL

PHP
Unit V, Module no.1

Content:
 PHP with MySQL database
o Steps for database connectivity
o Example of PHP script for connectivity with MySQL
 Small PHP project for signup and login

MYSQL DATABASES IN PHP

There are five steps to make PHP database interaction and I will explain each one in detail.
1. Create a connection
2. Select database
3. Perform database query
4. Use return data
5. Close connection

1. Create a connection
It is very important when you want to start a dynamic website to create a connection with
your SQL (we are talking about MySQL) by using mysql_connect() function. In
mysql_connect() arguments should be a string and it’s important to use die() function with
mysql_error() function to check if there is a problem with the connection or not.
2. Select database
Now we have to select the database which we are creating and saving our data by using
mysql_select_db() function which the arguments are the name of the database and connection
we made earlier.
3.  Perform database query
In this step, we have to select out data from the database and bring it into our web page. The
best decision to use mysql_query() function which it will Send a MySQL query with 2
arguments as displayed in the above code.
4. Use return data
To display the result on your web page, you have to use while() loop function in addition to
mysql_fetch_array() function which fetches a result row as an associative array, a numeric
array, or both.
5. Close connection
To close connection, it is better to use mysql_close() function to close MySQL
connection. This is a very important function as it closes the connection to the database
server. Your script will still run if you do not include this function. And too many open
MySQL connections can cause problems for your account. This is a good practice to close the
MySQL connection once all the queries are executed.
BCA III SEM & M.Sc(CS) IV Sem
PHP Unit V: Module No. 1
MYSQL

Step 2: Writing a PHP program to interact with the previously


created table

Program 1: cust.php
<html>
<body>
<form name="cust_info" method="post" action="save.php">
<h1>Customer information form</h1>
<hr>
Enter customer id<input type="number" name=id><br>
Enter customer name<input type="text" name=nm><br>
Enter City<input type="text" name=cy><br>
Enter Phone number<input type="number" name=ph><br>
<input type=submit>
</form>
</body>
</html>

Output:

Above program takes four values from the user and when user submit the form it invokes
save.php

Program 2: save.php

<html>
<body>
<?php
$cid=$_POST['id'];
$cnm=$_POST['nm'];
$city=$_POST['cy'];
$cph=$_POST['ph'];
$query="insert into customer(cust_id,cust_nm,city,phone)values(?,?,?,?)";
$values=array($cid,$cnm,$city,$cph);
BCA III SEM & M.Sc(CS) IV Sem
PHP Unit V: Module No. 1
MYSQL

$cdn="mysql:host=localhost;dbname=ashu_db";
$object=new PDO($cdn,"root","");
$ref=$object->prepare($query);
$ref->execute($values);
$n=$ref->rowcount();
echo "$n. record created";
?>
</body
</html>

Output:

Let’s try to create a signup form and a login form as a small project

Program:Signup.php:
<html>
<body>
<form action="signup1.php" method="POST">
<h1>Signup form</h1>
BCA III SEM & M.Sc(CS) IV Sem
PHP Unit V: Module No. 1
MYSQL

<br>
<label>Name</label><input type=text name="snm"><br>
<label>Address</label><input type=text name="sadd"><br>
<label>Phone</label><input type=text name="sph">
<label>Password</label><input type=password name="pwd"><br>
<input type=submit name=insert>
</form>
</body>
</html>

Output: Signup.php

Program:Signup1.php
<html>
<body>
<h1>User Created Successfully</h1>
<?php
$snm=$_POST['snm'];
$sadd=$_POST['sadd'];
$sph=$_POST['sph'];
$spwd=$_POST['pwd'];
if(empty($_POST["id"])){
echo"<Font color=red><h2>Customer id should not be blank</h2></font>";
include 'cust.php';
}
else
{
$query="insert into signup (name,address,phone,pwd) values (?,?,?,?)";
$values=array($snm,$sadd,$sph,$spwd);
$cdn="mysql:host=localhost;dbname=zee";
$object=new PDO($cdn,"root","");
$ref=$object->prepare($query);
$ref->execute($values);
$n=$ref->rowcount();
BCA III SEM & M.Sc(CS) IV Sem
PHP Unit V: Module No. 1
MYSQL

echo "$n. user Created";


}
?>
<form name=show action="login.php">
<input type="submit" value="Login now">
</form>
</body>
</html>

Output:Signup1.php

Program: Login.php

<html>
<body>
<form name="login" method="post" action="confpwd.php">
Enter Login: <Input type="text" name="nm1"><br>
Enter Password<Input type="password" name="pwd"><br><br><br>
<input type=submit value=Login>
</form>
<a href=signup.php>Signup</a><br>
<a href=login.php>Login</a>
</body>
</html>
Output:Login.php
BCA III SEM & M.Sc(CS) IV Sem
PHP Unit V: Module No. 1
MYSQL

Here, we are using mysqli instead of mysql.In mysqli the parameter definition of built-
in functions are different.

Program: Confpwd.php

<html><body>
<?php
$a=$_REQUEST["snm"];
$b=$_REQUEST["pwd"];
$connection=mysqli_connect("localhost","root","") or die("couldn't find server");
$db=mysqli_select_db($connection,"ashu_db") or die("couldn't find database");
$query="select name,pwd from signup where(name='$a')";
$result=mysqli_query($connection,$query) or die("Query failed");
$row=mysqli_fetch_array($result);

if(($row['name']==$a) and ($row['pwd']==$b))


echo "<font color='green'><h3>Welcome ",$a,"</h3></font>";
else
echo "<font color='red'><h3>Wrong password!! Try again, Please check the caps
lock</h3></font>";

mysqli_close($connection);
?>
<br>
<a href=login.php>Click to Login</a>
<br>
<a href=signup.php>Click to Signup</a>
</body></html>

Output: Confpwd.php
BCA III SEM & M.Sc(CS) IV Sem
PHP Unit V: Module No. 1
MYSQL

References
 https://www.w3schools.com/Php/default.asp
 https://www.tutorialspoint.com/php/index.htm
 https://www.javatpoint.com/php-tutorial

Edited & compiled by:


Mr. Zeeshan A Siddiqui
Assistant Professor
Department of Computer Applications
The Bhopal School of Social Sciences

You might also like