Saturday, October 23, 2010

Database Connectivity mysql and php

Hi to all!! I am back here with a new topic in mysql php.
Today i am here to teach you what is Database connectivity is??
The word DB Connectivity may look odd but it is very simple
it just needs few basic steps to connect your front end page(eg. PHP application) with the Database. We already know what Database is??

Database connectivity:
There are many ways to connect to db with frontend application.
step 1:
we need to declare the variables.
like:
$dbhost = 'localhost'; //your localhost name
$dbuser = 'root'; // user name(set for your local server)
$dbpass = 'password'; Password(set for your local server )
where $dbhost,$dbuser,$dbpass are the variables where we store the values of our localserver.
we can make use of these variables where we need them instead of the values.
step 2:
connect to database.
like:

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

where,

$conn is a varable holding the value of input command mysql_connect.
mysql_connect is the command used to connect the database.

the above syntax means that if the $dbhost,$dbuser,$dbpass values are true and correct coonect the database.

step 3:
but with what?? so we must declare a database available in phpmyadmin
consider i have a DB called "exampledb"

So declare a variable called

$dbname = 'exampledb';

step 4:
To check or confirm that such DB exists we use the below syntax.
mysql_select_db($dbname);

The above syntax returns false if such db doesnt exist.

I conclude,

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die 'Error connecting to mysql');

$dbname = 'db';// db is the name of the database.
mysql_select_db($dbname);
?>

We need to add the required functions in between the select db and ?> to work out.

I hope you have understood what mysql PHP connectivity is.

Thanks.

Pls post comments if you have any problems with above notes.

Good luck!!!

Wednesday, October 6, 2010

Sessions in PHP

Hi to all!! I am back with a new concept in PHP Sessions.

A PHP session variable is used to store information about, or change settings for a user session.

Session variables hold information about one single user, and are available to all pages in one application.


A PHP session solves this problem by allowing you to store user information on the server for later use (i.e. username, shopping cart items, etc). However, this session information is temporary and is usually deleted very quickly after the user has left the website that uses sessions.

It is important to ponder if the sessions' temporary storage is applicable to your website. If you require a more permanent storage you will need to find another solution, like a MySQL database.

Sessions work by creating a unique identification(UID) number for each visitor and storing variables based on this ID. This helps to prevent two users' data from getting confused with one another when visiting the same webpage.

Working with Sessions:
Before you can store user information in your PHP session, you must first start up the session.

This tiny piece of code will register the user's session with the server, allow you to start saving user information and assign a UID (unique identification number) for that user's session.

The code above will register the user's session with the server, allow you to start saving user information, and assign a UID for that user's session.

Storing a Session Variable

The correct way to store and retrieve session variables is to use the PHP $_SESSION variable:












Destroying a Session




unset($_SESSION['views']);
?>

other way to destroy the session variables:


session_destroy();
?>

classes and objects in PHP

Hi Friends,

Today i am here to present you

The hardest thing to learn (and teach btw,) in object oriented PHP … is the basics. But once you understand them, the rest will come much, much easier.

Classes and Objects are considered to be the most useful and dynamic aspects of a programming language.

Classes:

In PHP, classes are used extensively and are very useful. The concept of classes allows for better performance and more features.

Let me tell you what a class is??

The Class in PHP is basically the same as in other languages such as Java. The class definition begins with the keyword class, followed by a class name.

The form name can be any name except a reserved word or keyword in PHP. The class name is followed by a pair of curly braces which contain the definition of class members and methods.

Look at the example below.



Objects??

An Object is an enclosed bundle of variables and functions which is copied from a Class.

Objects provide an easy interface and hide a lot of their inner workings.

The object sends orders through special functions called methods and they can return information.

The class abc is the basis from which many objects can be instantiated. The new keyword is used to create an object.

Below simple Example shows who to use php classes and objects in php with html.
In the following example $obj1 and $obj2 are the objects of the class abc. In this $obj2 has been assigned the string “Welcome to My blog!!” to its $a property.

a = "Welcome to My blog!!";

echo "$obj1->a";
echo "$obj2->a";
?>

Note:
The new keyword is used to create an object. Now any abc object that is created contains a property called $a with the value of “Hello”. This property can be accessed and even be changed with the help of objects.


Extends



Classes and Objects are considered to be the most useful and dynamic aspects of a programming language. In PHP, classes are used extensively and are very useful. The concept of classes allows for better performance and more features.

What is a Class?
The Class in PHP is basically the same as in other languages such as Java. The class definition begins with the keyword class, followed by a class name. The form name can be any name except a reserved word or keyword in PHP. The class name is followed by a pair of curly braces which contain the definition of class members and methods.



Example of a Class






Class abc

{

//member functions and variables go here

}



?>


What is an Object?
An Object is an enclosed bundle of variables and functions which is copied from a Class. Objects provide an easy interface and hide a lot of their inner workings. The object sends orders through special functions called methods and they can return information.

While creating a Class, a set of characteristics is laid down. By creating Objects of that type, entities are created that share these characteristics but the Object might initialize them as different values.



Example



Suppose there is a class named building. This class would have a characteristic named floor. All the objects of class building would share the characteristics of floor, but some would initialize it to “one”, some to “two”, others to “three” or “four”, and so on.


The benefit of object oriented code is that it is re-useable. In this the classes can be used to create different objects and classes from one project can be used in other projects as well. Child classes can also be created which inherits the properties of the parent classes.

Creating an Instance
To start with, a class having no member functions and variables is not useful. For a class to be completely useful, member functions and variables have to be added in that class.

Let’s take an example of a class with a variable in it.



Example






Class abc

{

$a = “Hello!”;

}



?>


The class abc is the basis from which many objects can be instantiated. The new keyword is used to create an object. Now any abc object that is created contains a property called $a with the value of “Hello”. This property can be accessed and even be changed with the help of objects.

In this the -> operator is used to access or change the properties of the object.

In the following example $obj1 and $obj2 are the objects of the class abc. In this $obj2 has been assigned the string “Welcome to expertrating!” to its $a property.

Example












Class abc

{

var $a = "Hello";

}



$obj1 = new abc();

$obj2 = new abc();

$obj2->a = "Welcome to expertrating!";

echo "$obj1->a
";

echo "$obj2->a
";



?>








Extends

Another feature of object oriented programming is used in PHP, which is inheritance. In PHP a class a class can inherit methods, functions and members of other class by using the extends keyword in the declaration. In PHP it is not possible to inherit from multiple classes, a class can inherit from only one base class.

The class from which inheritance is done is called the parent class or base class and the class which inherits is called the child class.