Thursday, 18 June 2015

PHP program is to read a text file and display in HTML

This PHP program is to read a text file and display in HTML


This is my text file i have saved it in the same folder where my php program is. you can save it at any place but you have to give the complete path for the file





SOURCE CODE :

<!DOCTYPE html>
<html>
<body>

<?php
//vivek.txt is my file name replace it with your file name
$myfile = fopen("vivek.txt", "r") or die("Unable to open file!");
// Output one line until end-of-file
while(!feof($myfile)) {
   echo fgets($myfile) . "<br>";
}
fclose($myfile);
?>

</body>
</html>

OUTPUT :









PHP and MySQL Programming/Database Connectivity

  • You Must have to follow these 5 steps to to perform Database connectivity with php .
  • 1 Opening a Connection to a MySQL Database
  • 2 Creating a Query
  • 3 Retrieving data from a SELECT Query
  • 4 Closing a Database Connection
  • 5 Error Handling

Open Database connection

 $db = "database1";
 $link = mysql_connect("localhost", "username", "password");
 mysql_select_db($db, $link);

Creating a Query

// A Query without any returned data
 mysql_query ("INSERT INTO `table1` ('val1', 'val2')");
 // A Query with returned data
 $query = mysql_query("SELECT 
 Retrieving data from a SELECT Query * FROM `table1`");

Retrieving data from a SELECT Query

# --- Connect To Database ---
 $db = "db1";
 $link = mysql_connect("localhost", "user", "pass");
 mysql_select_db($db, $link);
 
 # --- Select Info from Database ---
 $result = mysql_query ("SELECT val1, val2 FROM tbl1");

//This will simply output the result in table-like format.
 while ($row = mysql_fetch_row($result)){
    foreach ($row as $field) {
       print "$field . ";
    }
    print "";
 }

Closing a Database Connection

  mysql_close();

Error Handling

print mysql_error();


PHP databse connection and embedding it with HTML

There are 5 simple steps we have to follow in order to make connection with database
1)create connection to database & test connection
2)perform database query & test for query errors
3)use returned data(if any)
4) release returned data
5)close database connection

example :

<?php
//1.create connnection to database
$dbhost="localhost";
$dbuser="root";
$dbpass="";
$dbname="mydb";
$connection=mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);
//test connnection
if(mysqli_connect_errno())
{
die("database connection failed: " . mysqli_connect_error().
" (".mysqli_connect_errno(). ")"
);
}

?>
<?php
//2.perform database query
$query ="SELECT * ";
$query .="FROM subjects ";
$result=mysqli_query($connection,$query);
//test for query errors
if(!$result)
{
die("database query failed");
}
?>
<html>
<head>
<title>database connection</title>
</head>
<body>
<table border="1">
<tr><th>sub1</th><th>sub2</th></tr>
<?php
//3.use returned data(if any)

while($row=mysqli_fetch_assoc($result))
{
?>
<tr><td><?php echo $row["sub1"] ; ?></td><td><?php echo $row["sub2"] ;?></td></tr>

<?php
}
?>
</table>
<?php
//4. release returned data
mysqli_free_result($result);
?>
</body>
</html>

<?php
//5.close database connection
mysqli_close($connection);
?>