Friday, 17 August 2007

Using Queries in PHP


This section, we will discuss more detailed about using queries in PHP for retrieving data from Mysql. As we know that for queri-ing something from Mysql in PHP, we must use mysql_query.

Let us see the following example:



NameGenderJob
JaneFemaleStudent
DianaFemaleStudent
JohnMaleStudent


According to the above table, if we want to show all data of the table. So we can use the following syntax:

include"connection.inc.php";
$strsql=mysql_query("SELECT * FROm t_student"); //assume that the table's name is t_student
while($data=mysql_fetch_array($strsql))
{
echo $data[name];
echo $data[gender];
echo $data[job];
}
?>

Connect Mysql in PHP

Previous section, we have already known that Mysql is a database for website. Now this section, we will discuss more detail about how to make a connection in PHP to Mysql for accessing data there.

Syntax for opening a connection in PHP is very simple as we see below:
$hostname="localhost";
$username="root"; // use username in phpmyadmin
$password="12345";// use password in phpmyadmin
$database="dblearn";// name of the database that we want to connect to
$strconnect=mysql_connect($hostname,$username,$password);
if($strconnect)
{
// if connection is made so we will select database to connect
mysql_selectdb($database,$strconnect);
}
else
{
// if connection is failed so we will display the error message
echo mysql_error();
}
?>


The syntax is very simple, isnt it? Remember to start php code, we must begin with .

Sunday, 12 August 2007

PHP and MySql

If we want to access or store information from or to the database and display in our website, we can use Mysql as our database. Actually PHP is compatible with several DBMS such as Mysql, Postgre, Oracle, and many more. But in this section, we will discuss more about Mysql because Mysql is an open source (free) database and easy to use because for querying, we also use SQL syntax.

Mysql is a popular database in developing website world because Mysql has several powerful plus point that make it become more popular than other such as:
1. Quick: since mysql was created, it was already designed to have quick access time in displaying data.
2. Not expensive: Mysql can be download freely.
3. Essy to use:we can interate with Mysql by using simple SQL syntax.
4. Run on any kinds of platform: Mysql can operate in whatever kinds of operating system such as Windows, Unix, Mac OS, etc.
5. Has several help tool: in internet, you can join with many kinds of mailing list about Mysql.

Nowadays Mysql has 3 versions such as 4.0 , 4.1 and 5.0. 5.0 version is still in developing stage, so it is not recommended to be used for making a big scale website.

The following are the simple syntax for running the PHP and Mysql:
1. mysql_connect is used to make or open a connection between PHP and Mysql.
2. mysql_query is used to run query in Mysql.
3. mysql_num_rows is used to know how many rows are affected with the SQL syntax.
4. mysql_fetch_array is used to fetch the data from the database, you can define what fields that you want to fetch its data.
5. mysql_fetch_row is used to fetch data per row from the database.
6. mysql_close is used to close a connection.

Queries


Queries is a question or request.
With MySQL, we can query a database for specific information and have a recordset returned.

For example:
Look at this below table:

NameAddressTelpCountry


JaneGolden Street no 71234564England


DianaBueno Vista98745356Singapore


If we use the queries like "SELECT * FROM t_customer WHERE country='Singapore'"
Then the result will be shown Diana Bueno Vista 98745356 Singapore

Thursday, 9 August 2007

Introduction PHP

Developing a website using HTML is an ordinary and static website. If you are a webmaster, you will know how tough and disadvantages of making website using HTML. First, the content in the website will always like that (can not change dynamically as long as you do not edit it). Second, HTML website does not allow user to interate with our website, wherever user just can be our audience to see our website. How boring rite??

In 1994 Rasmus Lerdolf invented and launched PHP as a monitoring tool of his website. PHP stands for PHP Hypertext Preprocessor.PHP is open source web based programming language.Since 1994, PHP is getting more popular because PHP has several advantaged such as :
1. Easy to use: syntax of PHP is easy to learn, so for non programmer do not need to be afraid when wanna learn it.
2. Free platform: PHP can operate in any kinds of operating system such as Windows, Linux, Mac OS etc.
3. Free: you can use PHP freely (without paying).
4. Have many source or guidelines: you can join many kinds of mailing list or discussion group like at www.php.net
5. Secure: other people can not see your PHP code because PHP is web based programming. Not like javascript, javascript can be seem by people in their website because javascript is client based programming.

According to the data in website, nowadays there are 13 million website using PHP as their website programming language and this will be increase in upcoming days.Now PHP has version 4 and 5. PHP 5 is more secure than PHP 4 because PHP 5 use Object Oriented method.If you are already used to object oriented,so PHP5 is the best PHP version for you.

Friday, 3 August 2007

Part IV A J A X Request a Server

AJAX - Sending a Request to the Server

To send off a request to the server, we use the open() method and the send() method.

The open() method takes three arguments. The first argument defines which method to use when sending the request (GET or POST). The second argument specifies the URL of the server-side script. The third argument specifies that the request should be handled asynchronously. The send() method sends the request off to the server. If we assume that the HTML and ASP file are in the same directory, the code would be:

xmlHttp.open("GET","time.asp",true);
xmlHttp.send(null);


Now we must decide when the AJAX function should be executed. We will let the function run "behind the scenes" when the user types something in the username text field:
<form name="myForm">
Name: <input type="text"
onkeyup="ajaxFunction();" name="username" />

Time: <input type="text" name="time" />
</form>


Our updated AJAX-ready "testAjax.htm" file now looks like this:
<html>
<body>

<script type="text/javascript">
function ajaxFunction()
{
var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
document.myForm.time.value=xmlHttp.responseText;
}
}
xmlHttp.open("GET","time.asp",true);
xmlHttp.send(null);
}
</script>


<form name="myForm">
Name: <input type="text"
onkeyup="ajaxFunction();" name="username" />
Time: <input type="text" name="time" />

</form>

</body>
</html>

Thursday, 2 August 2007

Part III Of A J A X (AJAX - The XMLHttpRequest Object)

AJAX - More About the XMLHttpRequest Object
Before sending data to the server, we have to explain three important properties of the XMLHttpRequest object.
The onreadystatechange Property
After a request to the server, we need a function that can receive the data that is returned by the server.

The onreadystatechange property stores the function that will process the response from a server. The following code defines an empty function and sets the onreadystatechange property at the same time:
xmlHttp.onreadystatechange=function()  {  
// We are going to write some code here }
The readyState Property
The readyState property holds the status of the server's response. Each time the readyState changes, the onreadystatechange function will be executed.
Here are the possible values for the readyState property:
StateDescription
0The request is not initialized
1The request has been set up
2The request has been sent
3The request is in process
4The request is complete
We are going to add an If statement to the onreadystatechange function to test if our response is complete (this means that we can get our data):
xmlHttp.onreadystatechange=function()  { 
if(xmlHttp.readyState==4) { // Get the data from the server's response } }
The responseText Property
The data sent back from the server can be retrieved with the responseText property.
In our code, we will set the value of our "time" input field equal to responseText:
xmlHttp.onreadystatechange=function()  {  if(xmlHttp.readyState==4)    {    document.myForm.time.value=xmlHttp.responseText;    }  }

Part II of A J A X

To understand how AJAX works, we will create a small AJAX application.
First, we are going to create a standard HTML form with two text fields: username and time. The username field will be filled in by the user and the time field will be filled in using AJAX.
The HTML file will be named "testAjax.htm", and it looks like this (notice that the HTML form below has no submit button!):
<html>
<body>
<form name="myForm">Name: <input type="text" name="username" />
Time: <input type="text" name="time" />
</form>
</body>
</html>
AJAX - Browser Support
The keystone of AJAX is the XMLHttpRequest object.
Different browsers use different methods to create the XMLHttpRequest object.
Internet Explorer uses an ActiveXObject, while other browsers uses the built-in JavaScript object called XMLHttpRequest.
To create this object, and deal with different browsers, we are going to use a "try and catch" statement.
You can read more about the try and catch statement in our JavaScript tutorial.
Let's update our "testAjax.htm" file with the JavaScript that creates the XMLHttpRequest object:
<script type="text/javascript">

function ajaxFunction()  
{
var xmlHttp;
  try    
     {
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
  catch (e)    
     {
// Internet Explorer
try
  {
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
       }    
  catch (e)      
 {
try
  {        
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
     catch (e)        
  {
alert("Your browser does not support AJAX!");
return false;
       }
}
}
}
</script>
<form name="myForm">
Name: <input type="text" name="username" />
Time: <input type="text" name="time" />
</form>
</body>
</html>
 
Example explained: First create a variable xmlHttp to hold the XMLHttpRequest object.
Then try to create the object with XMLHttp=new XMLHttpRequest().
This is for the Firefox, Opera, and Safari browsers. If that fails, try xmlHttp=new ActiveXObject("Msxml2.XMLHTTP") which is for Internet Explorer 6.0+, if that also fails, try xmlHttp=new ActiveXObject("Microsoft.XMLHTTP") which is for Internet Explorer 5.5+
If none of the three methods work, the user has a very outdated browser, and he or she will get an alert stating that the browser doesn't support AJAX.
Note: The browser-specific code above is long and quite complex. However, this is the code you can use every time you need to create an XMLHttpRequest object, so you can just copy and paste it whenever you need it. The code above is compatible with all the popular browsers: Internet Explorer, Opera, Firefox, and Safari.
The next chapter shows how to use the XMLHttpRequest object to communicate with the server.

Introduction About A J A X

AJAX stands for Asynchronous JavaScript And XML.AJAX is a type of programming made popular in 2005 by Google (with Google Suggest).AJAX is not a new programming language, but a new way to use existing standards.With AJAX you can create better, faster, and more user-friendly web applications.AJAX is based on JavaScript and HTTP requestsWhat You Should Already KnowBefore you continue you should have a basic understanding of the following:HTML / XHTML JavaScript If you want to study these subjects first, find the tutorials on our Home page.

AJAX = Asynchronous JavaScript and XMLAJAX is not a new programming language, but a technique for creating better, faster, and more interactive web applications.With AJAX, your JavaScript can communicate directly with the server, using the JavaScript XMLHttpRequest object. With this object, your JavaScript can trade data with a web server, without reloading the page.AJAX uses asynchronous data transfer (HTTP requests) between the browser and the web server, allowing web pages to request small bits of information from the server instead of whole pages.The AJAX technique makes Internet applications smaller, faster and more user-friendly.AJAX is a browser technology independent of web server software.

AJAX is Based on Web StandardsAJAX is based on the following web standards:JavaScript XML HTML CSS The web standards used in AJAX are well defined, and supported by all major browsers. AJAX applications are browser and platform independent.

AJAX is About Better Internet ApplicationsWeb applications have many benefits over desktop applications; they can reach a larger audience, they are easier to install and support, and easier to develop.However, Internet applications are not always as "rich" and user-friendly as traditional desktop applications.With AJAX, Internet applications can be made richer and more user-friendly.

AJAX Uses HTTP RequestsIn traditional JavaScript coding, if you want to get any information from a database or a file on the server, or send user information to a server, you will have to make an HTML form and GET or POST data to the server. The user will have to click the "Submit" button to send/get the information, wait for the server to respond, then a new page will load with the results.Because the server returns a new page each time the user submits input, traditional web applications can run slowly and tend to be less user-friendly.With AJAX, your JavaScript communicates directly with the server, through the JavaScript XMLHttpRequest objectWith an HTTP request, a web page can make a request to, and get a response from a web server - without reloading the page. The user will stay on the same page, and he or she will not notice that scripts request pages, or send data to a server in the background.The XMLHttpRequest ObjectBy using the XMLHttpRequest object, a web developer can update a page with data from the server after the page has loaded!AJAX was made popular in 2005 by Google (with Google Suggest).Google Suggest is using the XMLHttpRequest object to create a very dynamic web interface: When you start typing in Google's search box, a JavaScript sends the letters off to a server and the server returns a list of suggestions.The XMLHttpRequest object is supported in Internet Explorer 5.0+, Safari 1.2, Mozilla 1.0 / Firefox, Opera 8+, and Netscape 7.
To be Continued.....looking forward to the Part 2

by Google