knowledgediarybd.com

Your Virtual Knowledge Campus

Database Connection

Database Connection With MySQL database by PHP code  is a very important

task for data access and manipulation. That’s why database connection is the

precondition to do work with MySQL database.

Why we Connect with  MySQL Database?

Actually if we can not connect with MySQL database we can  not insert ,delete,update

any record or data.

How to connect MySQL Database:

Here  I provide a PHP code for connecting with MySQL database in local host. Before connect we have

to open  XAMPP Control panel and have to start Mysql & Apache just like below.

If the Mysql is not running than database could not  connect with Mysql database.

PHP Code For Database connection and Insertion a data:

<html>
<body>

<?php
/*connecting database*/
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

echo "Database Connected Successfully";
?>

</body>

</html>

 

To see live Just copy and past above code on your notepad and save as “database connection.php”

If you want to  check it you have to keep it (“database connection.php”)in C:/ drive then xampp

then htdocs.

finally for  run this file “database connection.php” write on your browser

http://localhost/database connection.php

Then you will see the following interface:

If Mysql server is OFF in XAMPP control panel. Then ywill see the following interface:

 Data Insertion in MySQL database:

Here is code for  Registration From for insertion data.

<html>
<head>

<title> Registration form</title>

</head>

<body>

<h1 align="center"></h1>

<form action="otherpageregistation.php" method="post"

<p align="center"></p>

</label>

<table width="728" height="535" border="3" align="center">
<tr>
<td width="641" height="481" align="center" valign="top" bordercolor="#990000" bgcolor="#9966FF"><h1 align="center"><U>Registration form</U></h1>

<label>
<div align="left">

<div align="center">Name:
<input name="name" type="text" id="name" />
<label>
Age:
<input name="age" type="text" id="age" size="10" />
</label>
</div>
<p align="center">
<label>E-mail
<input name="e-mail" type="text" id="e-mail" />
</label>
<label>password
<input name="password" type="text" id="password" />
</label>
</p>
<p align="center">
<label></label>
</p>

<table width="200" align="center">
<tr>
<td><label>
<input type="radio" name="sex" value="male" />
Male</label></td>
</tr>
<tr>
<td><label>
<input type="radio" name="sex" value="female" />
Female</label></td>
</tr>
</table>

<p align="center">Birthday:</p>
<div align="center">Day
<select name="day" size="1" id="day">
<option selected="selected">1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
<option>11</option>
<option>12</option>
<option>13</option>
<option>14</option>
<option>15</option>
<option>16</option>
<option>17</option>
<option>18</option>
<option>19</option>
<option>20</option>
<option>21</option>
<option>22</option>
<option>23</option>
<option>24</option>
<option>25</option>
<option>26</option>
<option>27</option>
<option>28</option>
<option>29</option>
<option>30</option>
<option>31</option>
</select>
</label>
<label>
Month
<select name="month" size="1" id="month">
<option selected="selected">1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
<option>11</option>
<option>12</option>
</select>
</label>
<label>
Year:
<select name="year" size="1" id="year">
<option>1990</option>
<option>1991</option>
<option>1992</option>
<option>1993</option>
<option>1994</option>
<option>1995</option>
<option>1996</option>
<option>1997</option>
<option>1998</option>
<option>1999</option>
<option>2000</option>
<option>2001</option>
<option>2002</option>
<option>2003</option>
<option>2004</option>
<option>2005</option>
<option>2006</option>
<option>2007</option>
<option>2008</option>
<option>2009</option>
<option>2010</option>
<option>2011</option>
<option>2012</option>
<option>2013</option>
</select>
</label>
</div>

<label>
<div align="center">
<input type="submit" value="Submit" />
</div>
</label>

</div>
</form>

</body>
</html>

The above code save as “reg.html” or your choice.

 Output of the above registration form:

The out put of the above code is look like below:

 A PHP Code that will work on above Registration form:

A php  code for data insert into MySQL database also you can see how data are being inserted into MySQL database.For this a database and a table have to create in php myadmin.

Here i create a database name: other and  a table name: otherpage with eight fields these are:

name, age,e-mail,password,sex,day,month,year.

save the following code as  “otherpageregistation.php”

<?php
$fname=$_POST['name'];
$fage=$_POST['age'];
$femail=$_POST['e-mail'];
$fpassword=$_POST['password'];
$fsex=$_POST['sex'];
$fday=$_POST['day'];
$fmonth=$_POST['month'];
$fyear=$_POST['year'];
$humayun=mysql_connect("localhost","root","");
if(!$humayun)
{
die('mysql can not connect'.mysql_error());
}
mysql_select_db("other",$humayun);
mysql_query ("INSERT INTO otherpage
VALUES('$fname','$fage','$femail','$fpassword','$fsex','$fday','$fmonth','$fyear')");
mysql_query ("delete from otherpage where password='fff'");
echo "your record successfully added ia data base";
mysql_select_db("other",$humayun);
$result=mysql_query("select *from otherpage");
echo "<table border='1'>
<tr>
<th>Name</th>
<th>age</th>
<th>email</th>
<th>Password</th>
<th>Sex</th>
<th>Day</th>
<th>Month</th>
<th>Year</th>

</tr>";
while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['age']."</td>";
echo "<td>".$row['e-mail']."</td>";
echo "<td>".$row['password']."</td>";
echo "<td>".$row['sex']."</td>";
echo "<td>".$row['day']."</td>";
echo "<td>".$row['month']."</td>";
echo "<td>".$row['year']."</td>";
echo "</tr>";

}

echo "</table>";

mysql_close($humayun);
//mail($femail,"hum","congratulation your registation has been successfully completed");

?>

After fulfill above registration form you have to press submit button and finally

you see the following output:

 Related Post or you may like:

knowledgediarybd.com © 2017
shares