Thursday, August 19, 2010

PHP and MySQL Introduction



PHP and MySQL Introduction

This tutorial covers the basics of accessing a MySQL database from your PHP script. You will learn how to connect to a MySQL database, and how to execute a SQL statement.

MySQL is a popular relational database management system that is commonly used in PHP-based web applications. The free version of MySQL is called "MySQL Community Server" and can be downloaded here: MySQL Downloads. Installation instructions are available here: Installing MySQL Community Server. For the purposes of this tutorial, we will access a database named "helloworld". This database will have a table called Widgets with the following structure.
Column Name Data Type Modifiers
id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT
name VARCHAR(32) NOT NULL
color VARCHAR(16) NOT NULL

The PHP language has built-in APIs for accessing MySQL servers. To access a database in MySQL from a PHP script, the first step is to connect to the MySQL server, using the mysql_connect() function. This function will return a connection identifier on success, and FALSE on failure. In the case of failure, the script can call mysql_error() to get an error message describing the failure.

// Replace these strings with the actual hostname:port,
// username, and password
$conn = mysql_connect("localhost:3306",
"username", "password");
if($conn === FALSE) {
die("Error connecting to database: "
. htmlspecialchars( mysql_error() ) );
}

The next step is to select which database the script will be accessing via the mysql_select_db() function. This function takes the name of the desired database, and returns TRUE on success and FALSE on failure.








// For this example we will connect to
// the 'helloworld' database
if(mysql_select_db('helloworld') === FALSE) {
die("Error selecting database: "
. htmlspecialchars( mysql_error() ) );
}

Now we are at the point where we can begin interacting with the database tables. The following code will select all records from our Widgets table, ordered by the name column. The mysql_query() function executes the SQL statement. For select statements, this function returns a result table on success, and FALSE on failure.

$statement = "select id, name, color from Widgets "
. "order by name";
$result = mysql_query($statement);
if($result === FALSE) {
die("Error executing statement: "
. htmlspecialchars( mysql_error() ) );
}

The following code will output an HTML table containing the contents of the Widgets table by iterating through the result table that was returned from mysql_query(). The mysql_fetch_assoc() function will be used to retrieve an associative array containing the next row of data from the result table. This function will return FALSE when all the rows have been retrieved.

// Iterate through the result table
echo('

Widgets

');
echo('');
echo('');
while($resultRow = mysql_fetch_assoc($result)) {

// The keys of the associative array
// are the column names
$id = $resultRow['id'];
$name = $resultRow['name'];
$color = $resultRow['color'];

echo('');

}
echo('
IDNameColor
' . htmlspecialchars($id) . ''
. htmlspecialchars($name) . '
'
. htmlspecialchars($color) . '
');

We'll finish up this example by freeing the result table and closing the connection.

mysql_free_result($result);
mysql_close($conn);

This introduction should give you enough information to get started with using MySQL from PHP.

Wednesday, August 18, 2010

PHP Array Concepts with Easy Examples

PHP Array Concepts with Easy Examples

Here I have discussed about array concepts in php.

I hope people reading this blog must have known the basics of Php

just for reference i am giving small introduction of what PHP is??

PHP ==' Hypertext Preprocessor'.

Open Source, Serverside scripting Language.

Used to generate dynamic Web pages.

PHP scripts resides between reserved PHP tags.



This allows the programmer to embed PHP scripts within HTML pages.

Structurally similar to C/C++

Supports Procedural and OOP concept.




All PHP statements ends with a semicolon



PHP variables must begin with a "$" sign.

ARRAYS IN PHP

Now comin array concepts:

Definition 1:
An array is simply a collection of keys and their associated values. In PHP, the key can be either an integer or string, while the value can be any PHP data type.


In simple words array can be defined as follows.

Definition 2:
Arrays are special data types. Despite of other normal variables an array can store more than one value.

CREATE AN ARRAY:

In PHP, Arrays are created using the array() function:

$blankArray = array();
Here $blankArray is a variable in to which we are storing the array values.
array(//give no of array elements.)

Initializing Array:

An array can be initialized with a set of values by passing the values as parameters to the array() function: $anArray = array('A', 'B', 'C', 'D');

When initializing an array in the manner above, the values are automatically associated with integer keys in sequential order, starting at zero. The keys can be specified explicitly using the => operator:

$myArray = array('firstKey' => 'A'

, 'secondKey' => 'B'
, 19 => 'C'
, 22 => 'D');

Retriving values from an array

Values can be retrieved from an array using the square bracket syntax below. If the key is a string, it is considered good practice to always use quotes.

echo $myArray['firstKey']; // A echo $myArray[19]; // C

Adding Values to an Array

Values can be added to an array using the syntax below:

$myArray['anotherKey'] = 'E';

If a key is not specified, the value will be associated with the largest previously assigned integer key plus one.

// If the largest integer key was 22, the key

// for the value below would be 23.
$myArray[] = 'F';

Removing Values from an Array

Use the unset() function to remove an item from an array.

unset($myArray[22]);  // Removes the item with key 22

Iterating Through an Array

The foreach operator can be used to iterate over the items in an array.

// Display the current contents of the array

foreach($myArray as $key => $value) {
echo $key . ' => ' . $value . '
';
}

Multi-Dimensional Arrays

In PHP, a multi-dimensional array can be created by making an array containing other arrays as values.

$multiDimensionalArray = array(

'A' => array(0 => 'red', 2 => 'blue', 3 => 'green'),
'B' => array(1 => 'orange', 2 => 'black'),
'C' => array(0 => 'white', 4 => 'purple', 8 => 'grey')
);

echo $multiDimensionalArray['A'][3]; // green
echo $multiDimensionalArray['C'][8]; // grey

here i have discussed few array concepts with their syntax.

I hope this post would give you the basic concepts of what an array is and how is it created and used.








Thursday, March 18, 2010

Keep your friends close and your enemies closer.

Raghav: Be good do good!!

Hi to all!!!

This is my first blog.