Friday, September 25, 2009
My SQL Database Connecting using 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?
Java Script Validation
Thursday, September 10, 2009
How to create validation image in php
How to Create Validation Image in PHP?
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); ?>