Latest Entries »

Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Wednesday, July 28, 2010

Easy way to select numerical data from MySQL table

I'll explain this using a example,


Lets thing we have table called EMP and column called SALARY.
Now suppose there are numerical data and string data in the SALARY column. (ex: 12000, 25000, not available, null...)
For this kind of columns we can use following method to select only numerical data;


SELECT SALARY FROM EMP WHERE (SALARY+0)!=0;

Monday, January 18, 2010

How to send HTTP request using PHP?

There is a predefined php class called HttpRequest. By using this class we can create object for handle HTTP requests. There are list of function to handle it. Refer PHP manual and get some idea about this class.

In WAMP before use this objects you need to make some changes in php.ini file. Add following line into the php.ini file (somewhere near in the extension).

"extension=php_http.dll"


Example
======

user HttpRequest object to handle get method http request

$r= new HttpRequest('www.example.com?valueone=1&valuetwo=2', HttpRequest::METH_GET);
$r->send();
echo $r->getResponseBody();

Sunday, December 20, 2009

Simple php sample code for privent SQL injection attacts


if(isset($_POST["un"]) && isset($_POST["pw"])){


mysql_connect("localhost","root","");
mysql_select_db("my_db");

$username = mysql_real_escape_string($_POST["un"]);
$password = mysql_real_escape_string($_POST["pw"]);

$sql = "SELECT * FROM user WHERE id = '$username' AND name = '$password';";
$result = mysql_query($sql);
if($result){
if(mysql_num_rows($result)>0){
echo "you loged in....";
}
}

}
?>



Id :

Password :


Tuesday, December 1, 2009

How to execute php files without loading it

This method will execute the file_handle.php file and return the html result. So we can simply use this to database updates using this (use get method for pass inputs).

echo "Start";

$result = file("http://localhost/testing/file_handle.php");

echo "end";
?>

Friday, October 23, 2009

Php function for search from a file

If you have the start and end points, you can get the content of that two points. (as a example if the start and end point are and then u can get the content of these tags using this function. eg: content)

","",$contents);
$t = my_strip("abc","xyz",$contents);

echo $t;
?>


aaa.txt
=====
ntbfjkb n jABCjergnv
fvb j njrbn ntbfjkb n jegnb ef
g45yyyXYZtrgjrgbXYZk oi iABCyji oni jij

output
=====
jergnv fvb j njrbn ntbfjkb n jegnb ef g45yyy

Tuesday, September 29, 2009

sample code for undestand php file handling functions

$comment = $_POST["txtcomment"];
if ($comment!=""){
$date=date('l dS \of F Y h:i:s A');
$massage = $date.": ".$comment."\n";

$file=fopen("comment.txt","a+") or exit("Unable to open file!");
fwrite($file,$massage) or die("Could not write to file");
fclose($file);
echo "Massage sent!";
}

?>

This will write the massage to the comment.txt file.