Duminda Chamara

Friday, September 25, 2009

My SQL Database Connecting using PHP

How Connect to a mysql database and read table data in PHP?


Use the following code to connect to a mysql database and retrieve retrieve data and print as a table.


First you have to create a connection to the mysql databse. use mysql_pconnect function to connect to the mysql databse. You have to specify host name (the server which is the database hosted) user name (dabase user name) and the password .


mysql_select_db function is use to select the mysql database that you want to connect. Here you have to specify valid mysql database name and the connection that mentioned above.


mysql_query function is use to execute a sql statement against the mysql databse.


mysql_fetch_array function is use to retrieve data from the executed sql query. It returns row by row during the execution in the while loop. 


mysql_close function is used to close the connection that have already open to the mysql database server.


<?php


$host = "localhost";//127.0.0.1
$user_name = "root";
$password = "1234";
$database = "test_db";


$con = mysql_pconnect($host,$user_name,$password);


if($con!=NULL){
$db=mysql_select_db($database,$con);

if($db!=NULL){
$sql  = "SELECT user_id,first_name,last_name FROM test_table";
$result = mysql_query($sql,$con);
print("<table border='1'> <th>User ID</th> <th>First Name</th> <th>Last Name</th>");
while($row=mysql_fetch_array($result)){
print("<tr>");
print("<td>".$row[0]."</td><td>".$row[1]."</td><td>".$row[2]."</tr>");
}
print("</table>");
mysql_close($con);
}else{
print("Database does not exists");
mysql_close($con);
}
}else{
print("Error: unable to connect");
}
?>

Monday, September 21, 2009

How to Get File Extension of a file in PHP


How to determine extension of a file in php?





When you are working with files you may need to get the extension of a file. In such case you can use following methods to get the file extension.


1. Example code for get the file extension in php.

<?php

/*

©-Copyright by kottawadumi.blogspot.com

*/


$file_name='C:\myfiles\myfile.doc.jpg';

$file_extension=pathinfo ($file_name,PATHINFO_EXTENSION);

echo $file_extension;

//Extension with dot '.'

echo ".".$file_extension;

?>

This example is based on pathinfo php inbuilt method. It is the easiest way to extract the extension from a file name. By specifying PATHINFO_EXTENSION parameter, you can directly get the extension of the file otherwise it returns the associative array containing the dirname, basename, extension,and filename.

Note that above pathinfo function returns the file extension without a dot "." So you have to manually add the dot before the extension.


2. Example code for determine the extension of a file in php.

<?php

/*

©-Copyright by kottawadumi.blogspot.com

*/


function get_file_extension($file_name){

$file = strtolower($file_name);

$extension = explode(".", $file_name);

return $extension[count($extension)-1];

}

$file_name='C:\myfiles\myfile.doc.jpg';



echo get_file_extension($file_name);



?>

In this example, divide the file name using dot "." delimiter and returns the last element of the array as the file extension.


3. Example for get the extension of a file using regular expression in php.

<?php

/*

©-Copyright by kottawadumi.blogspot.com

*/


$file_name='C:\myfiles\myfile.doc.jpg';

$pattern='/(\.[^.]+)$/';

preg_match($pattern,$file_name,$matches);

echo end($matches);



?>

In this example, It uses a regular expression to extract the file extension. It returns the file extension with the dot "."













©-Copyright By Duminda Chamara  
Java Script Validation  

Thursday, September 10, 2009

How to create validation image in php

How to Create Validation Image in PHP?

Following example shows you how to create a random validation image using PHP .
Sometimes you may have seen there are some web forms that prompt you to enter validation code before you submit the form. That image comtains a peice of text that somewhat difficult to read. It is a validation image.

Captcha image is use to to ensure that the response is not generated by a computer. The process asking a user to entera simple text which is generated by a computer, in the form of image file format (jpg or png format). This text only can recogize by a human but not computers. So that using Captcha images ensure that the request is made by a human and not by automated computer program. CAPTCHA stands for "Completely Automated Public Turing Test to tell Computers and Humans Apart."

Random validation image is an image that containing a little text, number or combination of numbers and letters. before you submit a form you have to enter the text in the image to make it valida submission. Random validation images are useful when avoid spammers troubles to your site. Think that your site has a forum, guest book or user registration form to intract with your website. If a spammeror robot submits that form automatically frequently, it makes your webserver bussy. Also it waste valuable server resources. You can protect your web application from spammers or robots by using random validation images. You can genereate a random validation image on the fly using simple PHP script.




1. Example code for create a random validation image in PHP.

PHP Code

<?php session_start();

$width=100; $height=40; $font_size=30; $image=imagecreate($width,$height); $black=imagecolorallocate($image,221,238,255); $border_color=imagecolorallocate($image,0,0,255); $font_color=imagecolorallocate($image,74,0,0); $font="fonts/Vrinda.ttf"; $text=rand(10000,90000); $_SESSION['validation_code']=$text; imagesetthickness ( $image, 1 );

//create a border of the validation image. imageline($image,0,1,$width,1,$border_color); imageline($image,($width-1),1,($width-1),($height-1),$border_color); imageline($image,0,($height-1),$width,($height-1),$border_color); imageline($image,0,1,0,($height-2),$border_color);

for($i=0;$i<10;$i++){ imageline($image,0,($i*4),$width,($i*5),imagecolorallocate($image,74+($i*2),(10+$i),($i*10))); } imagettftext($image,$font_size,0,8,30,$font_color,$font,$text); header("Content-Type: image/png"); imagepng($image); imagedestroy($image); ?>

©-Copyright By Duminda Chamara JavaScript Validation