connect PHP with database

<!DOCTYPE html>
<html lang=”en-US”>
<head>
<meta charset=”UTF-8″>
<title>fetch.php</title>
<style type = “text/css”>
table, th, td {border: 1px solid black};
</style>
</head>
<body>
<?php

try{
$connect=new PDO(‘mysql:hostname=localhost;dbname=learn’,”root”);
$connect->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$query=’SELECT * FROM CONTACT’;
print “<table> \n”;
$field=$connect->query($query);
$row=$field->fetch(PDO::FETCH_ASSOC);
print “<tr> \n”;
foreach ($row as $field=>$value){
print “<th> $field </th> \n”;
}
print “</tr> \n”;
$data=$connect->query($query);
$data->setFetchMode(PDO::FETCH_ASSOC);
foreach($data as $row){
print “<tr> \n”;
foreach($row as $name=>$value){
print “<td> $value </td> \n”;
}
print “</tr> \n”;
}
print “</table> \n”;
}
catch(PDOException $e) {
echo ‘ERROR: ‘ . $e->getMessage();
}
?>
</body>
</html>

Continue reading “connect PHP with database”

File Handling – PHP

Note: You can Download all .PHP Files directly here.

You can also find other Programs in About Page.

kg.php

<html>
<head><title>Kanulp’s Blog</title>
<style type=”text/css”>
#search
{
width: 100px;
height: 30px;
color:white;
background-color:blue;
}
#search:hover
{
width: 110px;
height: 30px;
color:black;
background-color:yellow;
}
.required{
color:red;
}
</style>
</head>
<body>
<?php
$error=0;
$success=””;
$nameq = $rateq = $yearq = $monthq = $dayq = $emailq=””;

if($_SERVER[“REQUEST_METHOD”]==”POST”){

if (ctype_alpha(str_replace(‘ ‘, ”, $_POST[“name”])) === false) {
$nameq=”Name Should consist only letters”;
$error=1;
}
else if (empty($_POST[“name”])){
$nameq=”Name is required”;
$error=1;
break;
}
else{
$error=0;
}

if(!isset ( $_POST[“trust”] ) ) {
$rateq = “For your safety rate this member!!!”;
$error=1;
}

if ( ! filter_var ( $_POST[“email”] , FILTER_VALIDATE_EMAIL ) ) {

$emailq = “Email is not valid”;
$error=1;
}

if(empty($_POST[“year”]))
{
$yearq=”Year is required for bday!”;
$error=1;
}

if(empty($_POST[“month”]))
{
$monthq=”Month is required”;
$error=1;
}

if(empty($_POST[“day”]))
{
$dayq=”Day is required!”;
$error=1;
}

if($error == 0)
{
$success = “Submitted successfully…”;

$d2 = $_POST[“year”];
$d1 = date(‘Y’);
$dif = $d1 – $d2;
$t = $_POST[“trust”];

switch($t)
{
case 1:$trate=”Trustable”;
break;

case “2”:$trate=”Beware to work with”;
break;

case “3”:$trate=”Think 100 times before work with”;
break;
}

$output = “Name:”.$_POST[“name”].”,”.”\tAge:”.$dif.”,”.”\tMastery:”.$_POST[“interest”].”,”.”\tEmail:”.$_POST[“email”].”,”.”IMP:”.$trate .”\n”;

$fp = fopen(“C:\wamp\www\k.csv”,”a”);
fwrite($fp,$output);
fclose($fp);
}
} ?>
<h2>Your Programing Team Details:</h2>
<form action=”<?php echo htmlspecialchars($_SERVER[“PHP_SELF”]);?>” method=”post” enctype=”multipart/form-data”>
<table>

<tr><td><lable>Name:</lable></td>
<td><input type=”text” name=”name”>

</td>
</tr>

<tr>
<td><lable>D.O.B:</lable></td>

<td><select name=”year”>
<option value=””>Year</option>
<?php for ($year = date(‘Y’); $year > date(‘Y’)-100; $year–) { ?>
<option value=”<?php echo $year; ?>”> <?php echo $year; ?></option>
<?php } ?>
</select>

<select name=”month”>
<option value=””>Month</option>
<?php for ($month = 1; $month <= 12; $month++) { ?>
<option value=”<?php echo strlen($month)==1 ? ‘0’.$month : $month; ?>”><?php echo strlen($month)==1 ? ‘0’.$month : $month; ?></option>
<?php } ?>
</select>

<select name=”day”>
<option value=””>Day</option>
<?php for ($day = 1; $day <= 31; $day++) { ?>
<option value=”<?php echo strlen($day)==1 ? ‘0’.$day : $day; ?>”><?php echo strlen($day)==1 ? ‘0’.$day : $day; ?></option>
<?php } ?>
</select><span class=”required”><?PHP echo $yearq.” “.$monthq.” “.$dayq; ?></span></td>

</tr>
<tr>
<td>Mastery:</td>
<td><select name=”interest”>
<option value=”php”>PHP</option>
<option value=”android”>Android</option>
<option value=”c++”>C++</option>
<option value=”java”>JAVA</option>
<option value=”html”>HTML & CSS</option>
</select></td></tr>

<tr>
<td>Email:</td>
<td><input type=”text” name=”email”><span class=”required”><?php echo $emailq ?></span></td>
</tr>

<tr>
<td>Trustable Rate:</td>
<td>
<input type=”radio” name=”trust” value=”1″>1
<input type=”radio” name=”trust” value=”2″>2
<input type=”radio” name=”trust” value=”3″>3
<span class=”required”><?php echo $rateq ?></span></td>
</tr>
</table>
<pre>
<input type=”submit” id=”search” name=”submit” value=”Done”></pre>

</form>

<?php

?>

</body>
</html>

So, I have used some CSS styles for hover and validation in for filing up.

Then I have set some validations explain in code comments.They will be executed when the submit button is clicked.

There is and variable called $output which basically contains strings what we have to store in our file.

File Handling functions.

To open a file in various modes: fopen();

in this function there are two parameter first we have to give path and second how we want to open file like in read/write/append mode ex. fopen(“C:\wamp\www\k.csv”,”a”);

I have used .CSV file. You can open .txt also. Now to write in files there is a function fwrite();

two parameters are 1.file path and 2.What to write in file.

Then close the file with fclose(filename);


Read From file.

readteams.php

<html>
<head><title>Your teams</title>
</head>
<body>
<h1>Teams</h1>

<?php

$fp = fopen(“C:\\wamp\\www\\k.csv”,”r”); #to open in windows

while(!feof($fp)){

$members = fgets($fp);
echo $members.”</br>”;

}
fclose($fp);
?>
</body>
</html>

To read from file we have to first open file in read mode

then with the help of feof(); and fgets(); we have read all lines.

The fgets() function stops at  EOF or we can set length like 110 to stop.


If you still have doubt can contact.