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.

Saturday, September 25, 2010

GET AND POST METHODS

Hi.. Today i am here to present u methods in php.

normally methods means orderly arrangement, elucidation, development, or classification.There are two ways the browser client can send information to the web server.
  • The GET Method
  • The POST Method
GET Method:

The GET method sends the encoded user information appended to the page request.

In other words GET methods is unprotected way to our query string.

  • The GET method is restricted to send upto 1024 characters only.
  • GET can't be used to send binary data, like images or word documents, to the server.
The PHP provides $_GET associative array to access all the sent information using GET method.

POST Method:

The POST method transfers information via HTTP headers. The information is encoded as described in case of GET method and put into a header called QUERY_STRING.

  • The PHP provides $_POST associative array to access all the sent information using GET method.
  • The POST method does not have any restriction on data size to be sent.
Example:

     echo "You are ". $_POST['age']. " years old.";      exit();   } ?> 

$_REQUEST variable:

The PHP $_REQUEST variable can be used to get the result from form data sent with both the GET and POST methods.

The PHP $_REQUEST variable contains the contents of both $_GET, $_POST, and $_COOKIE.

Example:

echo "You are ". $_REQUEST['age']. " years old.";      exit();   } ?> 

Friday, August 20, 2010

Indexing in MySql


Hello Friends,

Here i am with another new concept in MySql Index.


Do you understand what an index is and how it works?

An index is an ordering of (part of) the columns in a row to improve access speed to a specific row or subset of rows. As others have said, the purpose is performance.

There are two ways to implement an index:
(1) logically arrange the rows themselves according to the sequence defined by the index. This is a "clustered" index. Since the rows can only be arranged in one sequence, there can only be one "clustered" index.
(2) leave the rows alone, but create a separate structure made up of only the indexed columns and a reference to the actual row. Order these according to the sequence of the index. This is a "non-clustered" index. (Also known as "alternate index" or "secondary index".) This index has no bearing on the way the data rows are organized.

When you access data through a "non-clustered" index, the system does two record lookups: first, it searches the index to find the index columns that meet the predicate criteria. When it finds one, it uses the reference to go to the table itself and retrieve the actual row.

When you access data through a "clustered" index, the system does one record lookup: it searches the index to find the index columns that meet the predicate criteria. When it finds one, the rest of the columns (the actual row) are stored with the index.

So a clustered index is faster than a non-clustered index, but you can only have one. A non-clustered index is faster than reading the whole table if you are looking for a very small percentage of the rows. If you are looking for a large portion of the rows, it may be faster to skip the index and just read the whole table.

All of this is in the physical design of the table.

The primary key, on the other hand, is in the logical design of the table. The primary key is unique -- there can only be one row with a given primary key value. It identifies exactly one row.

In most databases, it makes sense to implement the primary key as a clustered index. Not all databases do this -- some implement the primary key as a non-clustered index, separate from the data rows.

Finally -- in some databases, a uniqueness constraint is implemented using a non-clustered index. This special-purpose index is not for performance reasons; rather, it is a physical mechanism for enforcing uniqueness.

In addition to the above post Here i am adding a URL which would guide you to the best knowledge in INDEXING Concepts in MySql. I went through the below link and it was really useful.

http://sqlserverpedia.com/wiki/Indexing_Strategies


MySql Basics


Hi friends.,

I my previous chapters i have discussed about what Mysql is and its invention. In this chapter i would tell you from where to download mysql and some links related to it. i shall also give you the exact syntax that must be used to execute Mysql commands.

Frankly, Mysql commands are very easy to understand and work. Let me start of the introduction to mysql downloads.

Use this link to download MySql.
http://dev.mysql.com/downloads/mysql/

once you have installed MySql in you PC

To start or stop MySQL use the following commands:
  • scripts/mysql.server stop
  • scripts/mysql.server start

INTRODUCTION TO DATABASE DESIGN:

General Database and Table Properties:

Every MySQL server has a data directory under which it manages the contents of databases and
tables. The server represents these using directories and files under the data directory as follows:

MySQL associates each database with a directory under the data directory. (This means
that the data directory is the parent of all database directories.) A database directory has
the same name as the database that it represents. For example, a database named world
corresponds to a directory named world under the data directory. MySQL uses the
database directory to manage the components of the database—that is, its tables and
indexes.

A database may be empty, or have one or more tables. Databases cannot be
nested; one database cannot contain another.

Each table in a database consists of rows and columns. A table can be empty (it can have
zero rows of data), but it must have at least one column. A table may also be indexed to
improve query performance.

Every table is associated with a format file in the database
directory that contains the definition, or structure, of the table. The format filename is the
same as the table name, plus an .frm suffix.

For example, the format file for a table named
Country in the world database is named Country.frm and is located in the world
directory under the server's data directory. Depending on the table type, the storage
engine for a table might create additional files for the table.

Storage Engines and Table Types:

1.MyISAM Tables

The MyISAM storage engine manages tables that have the following characteristics:

Each MyISAM table is represented on disk by an .frm format file, as well as an .MYD
data file and an .MYI index file. All these files are located in the database directory.

MyISAM has the most flexible AUTO_INCREMENT column handling of all the table
types.

MyISAM tables can be used to set up MERGE tables.

MyISAM tables can be converted into fast, compressed, read-only tables.

MyISAM supports FULLTEXT searching.


2.InnoDB Tables

1.The InnoDB storage engine manages tables that have the following characteristics:
InnoDB table is represented on disk by an .frm format file in the database directory,

2. InnoDB supports transactions (using the SQL COMMIT and ROLLBACK statements)
with full ACID compliance.

3. InnoDB provides auto-recovery after a crash of the MySQL server or the host where the
server runs.

4. InnoDB supports foreign keys and referential integrity, including cascaded deletes and
updates.

5. MySQL manages query contention for InnoDB tables using multi-versioning and rowlevel
locking.

3. MERGE Tables

1. A MERGE table is a collection of identically structured MyISAM tables. Each MERGE
table is represented on disk by an .frm format file and an .MRG file that lists the names of
the constituent MyISAM files. Both files are located in the database directory.

2. Logically, a query on a MERGE table acts as a query on all the MyISAM tables of which it
consists.

3. A MERGE table creates a logical entity that can exceed the maximum MyISAM table size.


4.BDB (Berkeley DB) Tables

1.Each BDB table is represented on disk by an .frm format file and a .db file that stores data
and index information. Both files are located in the database directory.

2 BDB supports transactions (using the SQL COMMIT and ROLLBACK statements) with
full ACID compliance.

3.BDB provides auto-recovery after a crash of the MySQL server or the host where the
server runs.

4. MySQL manages query contention for BDB tables using page-level locking.

5.HEAP (MEMORY) Tables

1. Each HEAP table is represented on disk by an .frm format file in the database directory.
Table data and indexes are stored in memory.

2. In-memory storage results in very fast performance.

3.HEAP table contents do not survive a restart of the server. The table structure itself
survives, but the table contains zero data rows after a restart.

4. HEAP tables use up memory, so they should not be used for large tables.

5 MySQL manages query contention for HEAP tables using table-level locking. Deadlock
cannot occur.


Identifier Syntax

When you write SQL statements, you use names to refer to databases and tables as well as to
elements of tables such as columns and (sometimes) indexes. It's also possible to create aliases,
which act as synonyms for table and column names. All of these types of names are known as
identifiers.

Parameters to create Identifiers


Identifiers for databases, tables, columns, and indexes may be unquoted or quoted. An unquoted
identifier must follow these rules:

1. An identifier may contain all alphanumeric characters, the underline character (_), and
the dollar sign ($).

2. An identifier may begin with any of the legal characters, even a digit. However, it's best
to avoid identifiers that might be misinterpreted as constants.
For example, 1e3 might be taken as a number in scientific notation, whereas 0x1 might be interpreted as a hex constant, so neither is a good choice for an identifier.

3. An identifier cannot consist entirely of digits.



An alias identifier can include any character, but should be quoted if it's a reserved word (such as
SELECT or DESC), contains special characters, or consists entirely of digits. Aliases may be
quoted within single quotes ( '), double quotes, or back ticks.


Using Qualifiers for Table and Column Names


Column and table identifiers can be written in qualified form—that is, together with the identifier
of a higher-level element, with a period (.) separator.
A table name may be qualified with the name of the database to which it belongs. For example,
the Country table in the world database may be referred to as world.Country (note the .
separating the two identifiers in the name).

W
ell friends i this post i have discussed some topics that are very miniute but essential to be kept in mind during writting the code in mysql. In my next post i shall move into the concepts of mysql coding such as creating a database,table etc...

Mysql concepts made easy

MySQL INTRODUCTION
Mysql made easy to understand and with simple examples

What is Mysql????

MySQL, pronounced "my Ess Que El," is an open source, Enterprise-level, multi-threaded, relational database management system.

MySQL was developed by a consulting firm in Sweden called TcX. They were in need of a database system that was extremely fast and flexible. Unfortunately (or fortunately, depending on your point of view), they could not find anything on the market that could do what they wanted. So, they created MySql, which is loosely based on another database management system called MySql.

The product they created is fast, reliable, and extremely flexible. It is used in
many places throughout the world. Universities, Internet service providers and nonprofit organizations are the main users of MySQL, mainly because of its price (it is mostly free).

Lately, however, it has begun to permeate the business world as a reliable and fast database system. The reason for the growth of MySQL's popularity is the advent of the Open Source Movement in the computer industry.

The Open Source Movement, in case you haven't heard about it, is the result of several computer software vendors providing not only a product but the source code aswell.

Now lets us know what is Database????




A database is a series of structured files on a computer that are organized in a highly efficient manner.

These files can store tons of information that can be manipulated and called on when needed.

A database is organized in the following hierarchical manner, from the top down. You start with a database that contains a number of tables. Each table is made up of a series of columns. Data is stored in rows, and the place where each row intersects a column is known as a field.

let us know what are the Features of MySql:

Features of MySQL
Relational database management system
  • Multithreaded server
  • Adheres to the standard set by the American national standards institute for sql
  • Online help system
  • Has Many APIs
  • Portability
So guys i shall discuss about download and Working with mysql Basic commands in my next post.