PHP session Tutorial


PHP SESSION Tutorial




In this tutorial i exaplain how to use session in php to get value in other page.

session is a way to get value in multiple pages without passing value from one page to another.

Let Start

Filename:- form.php
<html>
<head></head>
<body>
<form action=”show.php” method=”post”>
<label>User</label>
<br>
<input type=”text”name=”user”>
<br>
<input type=”submit” value=”submit”>
<!--here it was just an html form to transfer value to one page to other-->
</form>
</html>


Filename :-show.php
<?php
session_start();

//To use session we must start our session.session_start() is used to start our session.

$user=$_REQUEST[‘user’];
$_SESSION[‘user’]=$user;

//Set value in $_SESSION variable with key user now we get value from session in any page by session.

echo “Hello ”.$user;
echo “<br>”;
echo “<a href=’session.php’>Go To Next Page</a>


?>

Filename:- session.php
<?php
session_start();

//Again started session to get the value from session variable.

echo “Again Hello ”.$_SESSION[‘user’];

//here we accessing the session variable value which we set in show.php

echo “<a href=’destroy.php’>Do You Want To Destroy Session</a>”;

//session destroying link
?>

Filename :-destroy.php

<?php
session_start();
session_destroy();
//here we destroy session by session destroy.
?>


Thank you in next Tutorial i will share how to create a login system.
8

PHP Mysql Tutorial



PHP MySql Tutorial





In this tutorial i will share how to use mysql and php.

Lets get start.

For Cpanel
Open Mysql Database And Create A New Database And Create a New Username and Password then set the username to Database And Give all privilages.
Now Open Your Phpmyadmin from cpanel then select database and create a new table which we use to store data.

In this tutorial i will create a table name login
And Columns are
id - int - 255 - Auto increment - primary key
name - text
password - text
email - text

For xampp:

Open http://localhost/phpmyadmin
And Create a new database and create the table same as which we created in for server.

Now Lets start How to insert,select,update,delete data from database.

Create Database Connection in PHP

Filename: connect.php

<?php
$server=”localhost”;
$username=”root”;
$password=”root”;
$database=”demo”;
$con=mysql_connect($server,$username,$password);

//this function is user to connect our server mysql it takes three parameters first is your server ,second is username and third parameter is password.

$con2=mysql_select_db($database,$con);

//this function is used to select our database. it takes two parameter first parameter is database name and second is the connection which we create to connect our server.
?>



Insert Operation

<?php
include ‘connect.php’;
//inclufe function is used to to include one file into other in php

$name=”sswebtricks”;
$email=”sswebtricks@gmail.com”;
$password=”pass@123”;

$qr=mysql_query(“INSERT INTO login (name,email,password) VALUES (‘$name’,’$email’,’$password’));

//mysql_query is function to write our query it takes one parameter which is our query it returns boolean type value true or false

If($qr)
{
echo “insert sucess”;
}
else
{
echo “failed”;
}
?>


Select Operation

<?php
include ‘connect.php’;
$name=”sswebtricks”;

$qr=mysql_query(“select * from login”);
$row=mysql_fetch_array($qr);

//mysql fetch array function is used to fetch fetch all the data from.database it takes one parameter which is our query and return array value .Here we only fetch single row .

echo $row[‘email’].”<br>”;
//print the email of name

//Now we fetch multiple rows
while($row2=mysql_fetch_array($qr))
{
  echo $row2[‘email’].”<br>”;
}

?>


Update Operation

<?php
include ‘connect.php’;
$name=”sswebtricks”;
$newemail=”sksanjeev362@gmail.com”;

$qr=mysql_query(“UPDATE login SET email='$newemail' WHERE name=’$name’");

// Above is updAte query

if(mysql_affected_rows>0)
{
echo “updated”;
}
else
{
echo “failed”;
}


//mysql affected rows check how many rows will affected with our query
?>

Delete Operation

<?php
include ‘connect.php’;
$name=”sswebtricks”;

$qr=mysql_query(“delete from login where name=’$name’”);

if($qr)
{
echo “ deleted”;
}
else
{
echo “failed”;
}
?>


8