<?php
//Advanced php date() format the date
echo "<br/>".date("Y/m/d'")."<br/>";
echo date("Y.m.d")."<br/>";
echo date("Y-m-d");
//getting tomorrow date
//mktime(hour,minute,second,month,day,year,is_dst) syntax
$tomorrow = mktime(0,0,0,date("m"),date("d")+1,date("Y"));
echo "Tomorrow is ".date("Y/m/d", $tomorrow);
//php cookies
//setcookie(name, value, expire, path, domain);
//php session
//php session_start();
//Exception Handling
//create function with an exception
function checkNum($number)
{
if($number>1)
{
throw new Exception("Value must be 1 or below");
}
return true;
}
//trigger exception in a "try" block
try
{
checkNum(2);
//If the exception is thrown, this text will not be shown
echo 'If you see this, the number is 1 or below';
}
//catch exception
catch(Exception $e)
{
echo 'Message: ' .$e->getMessage();
}
//MySql & php
//open a connection
$con=mysqli_connect("127.0.0.1","shankar_pc","456321aA","Occumen");
if(mysqli_connect_errno($con))
{
echo"Failed to connect to MySql:".mysqli_connect_error();
}
$sql="create DATABASE MY_DB";
if(mysqli_query($con,$sql))
{
echo"database my_db created successfully..";
}
else
{
echo"error creating database".mysqli_error();
}
//close a connection
$con1=mysqli_connect("192.168.1.124","shankar_pc","456321aA","Occumen");
if(mysqli_connect_errno($con))
{
echo"Failed to connect to Mysql:".mysqli_connect_error();
}
mysqli_close($con);
//creating table
$connection=mysqli_connect("192.168.1.124","shankar_pc","456321aA","Occumen");
//check connection
if(mysqli_connect_errno($connection))
{
echo"connecting mysqli is failed".mysqli_connect_error();
}
//creating table
$sql="CREATE TABLE Persons(FirstName CHAR(30),LastName CHAR(30),Age INT)";
/*primary key and auto increment field
$sql = "CREATE TABLE Persons
(
PID INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(PID),
Firstname CHAR(15),
Lastname CHAR(15),
Age INT
)"; */
if(mysqli_query($connection,$sql))
{
echo"table Persons is created successfully";
}
else
{
echo"Error creating table".mysqli_error();
}
//insert statement
$con=mysqli_connect("example.com","shankar-pc","456321aA","Occumen");
//check connection
if(mysqli_connect_errno($con))
{
echo"connecting mysqli is failed".mysqli_connect_error();
}
mysqli_query($con,"INSERT into Emp(Ename,Sal)values('shankar',10000)");
mysqli_close($con);
//Insert data into database from form
/*<html>
<body>
<form action="insert.php" method="post">
Firstname: <input type="text" name="firstname">
Lastname: <input type="text" name="lastname">
Age: <input type="text" name="age">
<input type="submit">
</form>
</body>
</html>*/
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO Persons (FirstName, LastName, Age)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error());
}
echo "1 record added";
mysqli_close($con);
//select data from DB
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Persons");
while($row = mysqli_fetch_array($result))
{
echo $row['FirstName'] . " " . $row['LastName'];
echo "<br />";
}
mysqli_close($con);
//display the data in the form of html table
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Persons");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
//where condition for table
$result = mysqli_query($con,"SELECT * FROM Persons
WHERE FirstName='Peter'");
//order by condition
$result = mysqli_query($con,"SELECT * FROM Persons ORDER BY age");
//update command
mysqli_query($con,"UPDATE Persons SET Age=36 WHERE FirstName='Peter' AND LastName='Griffin'");
//delete command
mysqli_query($con,"DELETE FROM Persons WHERE LastName='Griffin'");
//PHP XML
//Ajax introduction in php
?>
No comments:
Post a Comment