Friday, 18 December 2015

Getting Data From MySQL Database

Data can be fetched from MySQL tables by executing SQL SELECT statement through PHP function mysql_query. You have several options to fetch data from MySQL.
The most frequently used option is to use function mysql_fetch_array(). This function returns row as an associative array, a numeric array, or both. This function returns FALSE if there are no more rows.
Below is a simple example to fetch records from employee table.

Example

Try out following example to display all the records from employee table.
<?php
   $dbhost = 'localhost:3036';
   $dbuser = 'root';
   $dbpass = 'rootpassword';
   
   $conn = mysql_connect($dbhost, $dbuser, $dbpass);
   
   if(! $conn )
   {
      die('Could not connect: ' . mysql_error());
   }
   
   $sql = 'SELECT emp_id, emp_name, emp_salary FROM employee';
   mysql_select_db('test_db');
   $retval = mysql_query( $sql, $conn );
   
   if(! $retval )
   {
      die('Could not get data: ' . mysql_error());
   }
   
   while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
   {
      echo "EMP ID :{$row['emp_id']}  <br> ".
         "EMP NAME : {$row['emp_name']} <br> ".
         "EMP SALARY : {$row['emp_salary']} <br> ".
         "--------------------------------<br>";
   }
   
   echo "Fetched data successfully\n";
   
   mysql_close($conn);
?>
The content of the rows are assigned to the variable $row and the values in row are then printed.
NOTE − Always remember to put curly brackets when you want to insert an array value directly into a string.
In above example the constant MYSQL_ASSOC is used as the second argument to mysql_fetch_array(), so that it returns the row as an associative array. With an associative array you can access the field by using their name instead of using the index.
PHP provides another function called mysql_fetch_assoc() which also returns the row as an associative array.

Example

Try out following example to display all the records from employee table using mysql_fetch_assoc() function.
<?php
   $dbhost = 'localhost:3036';
   $dbuser = 'root';
   $dbpass = 'rootpassword';
   
   $conn = mysql_connect($dbhost, $dbuser, $dbpass);
   
   if(! $conn )
   {
      die('Could not connect: ' . mysql_error());
   }
   
   $sql = 'SELECT emp_id, emp_name, emp_salary FROM employee';
   mysql_select_db('test_db');
   $retval = mysql_query( $sql, $conn );
   
   if(! $retval )
   {
      die('Could not get data: ' . mysql_error());
   }
   
   while($row = mysql_fetch_assoc($retval))
   {
      echo "EMP ID :{$row['emp_id']}  <br> ".
         "EMP NAME : {$row['emp_name']} <br> ".
         "EMP SALARY : {$row['emp_salary']} <br> ".
         "--------------------------------<br>";
   }
   
   echo "Fetched data successfully\n";
   
   mysql_close($conn);
?>
You can also use the constant MYSQL_NUM, as the second argument to mysql_fetch_array(). This will cause the function to return an array with numeric index.

Example

Try out following example to display all the records from employee table using MYSQL_NUM argument.
<?php
   $dbhost = 'localhost:3036';
   $dbuser = 'root';
   $dbpass = 'rootpassword';
   
   $conn = mysql_connect($dbhost, $dbuser, $dbpass);
   
   if(! $conn )
   {
      die('Could not connect: ' . mysql_error());
   }
   
   $sql = 'SELECT emp_id, emp_name, emp_salary FROM employee';
   mysql_select_db('test_db');
   $retval = mysql_query( $sql, $conn );
   
   if(! $retval )
   {
      die('Could not get data: ' . mysql_error());
   }
   
   while($row = mysql_fetch_array($retval, MYSQL_NUM))
   {
      echo "EMP ID :{$row[0]}  <br> ".
         "EMP NAME : {$row[1]} <br> ".
         "EMP SALARY : {$row[2]} <br> ".
         "--------------------------------<br>";
   }
   
   echo "Fetched data successfully\n";
   
   mysql_close($conn);
?>
All the above three examples will produce same result.

Releasing Memory

Its a good practice to release cursor memory at the end of each SELECT statement. This can be done by using PHP function mysql_free_result(). Below is the example to show how it has to be used.

Example

Try out following example
<?php
   $dbhost = 'localhost:3036';
   $dbuser = 'root';
   $dbpass = 'rootpassword';
   
   $conn = mysql_connect($dbhost, $dbuser, $dbpass);
   
   if(! $conn )
   {
      die('Could not connect: ' . mysql_error());
   }
   
   $sql = 'SELECT emp_id, emp_name, emp_salary FROM employee';
   mysql_select_db('test_db');
   $retval = mysql_query( $sql, $conn );
   
   if(! $retval )
   {
      die('Could not get data: ' . mysql_error());
   }
   
   while($row = mysql_fetch_array($retval, MYSQL_NUM))
   {
      echo "EMP ID :{$row[0]}  <br> ".
         "EMP NAME : {$row[1]} <br> ".
         "EMP SALARY : {$row[2]} <br> ".
         "--------------------------------<br>";
   }
   
   mysql_free_result($retval);
   echo "Fetched data successfully\n";
   
   mysql_close($conn);
?>
While fetching data you can write as complex SQL as you like. Procedure will remain same as mentioned above. 

Monday, 7 March 2011

Various Trends Related to Web Application Development

The World Wide Web (WWW) has changed how today's businesses communicate. WWW is not anymore only used to get and disseminate information but employed as a tool for performance. Today's websites are not just static pages but interactive tools that enhance your business capabilities in real time. These responsive sites are powered by the web-based applications that are capable of responding and interacting with the user and allow skimming the information that is relevant and is up to date.

The professional web development team integrates most powerful and sophisticated technology into client's web site creating a faster web application reaching global users in no time. A technologically upgraded web application helps in executing things faster and smoother.

Here is a look at the most popular web based application development language options:

PHP

PHP is arguably the most preferred open source language used by the web developers. The choice is driven by the flexibility and plainness that the language offers. The language was developed primarily by people coming together as community members in a bid to make the language more efficient and successful in its usage. They best thing about PHP is that it is absolutely free of cost. The developers have only the best thing to say about the language because it is usually updated on a more regular basis than other language options.

The language is reported to have some problems, like a lack of case sensitivity and the presence of some event based errors. However, they are best ignored in front of the advantages that the language has to offer.

ASP.NET

ASP.NET is often acknowledged as an easily adaptable web application language. It can easily be applied with other languages like C, Lisp, VB, Cobol as well as some scripted language like Python, VBScript and Jscript, among others.

The language is also known to work well with C++ Builder, VisualStudio.NET as well as WebMatrix.

ASP.Net also has some inherent drawbacks. It is known to be somewhat slower in operation. Also it is known to be pretty complicated than other languages.

Selecting a web application development language is a major task for programmers, because as we can find many techniques, tools and methods to expand diverse websites. As different application does different types of tasks, it has almost become impracticable for a website developer to choose for any meticulous web applications development language.

A critical aspect related to application development is that it should be built in a way that it gets an ability to manage active sessions well.