Tuesday, March 16, 2010

Use of foreach loop php

We can take form submision value with foreach loop easily without writting loots of time $name=$_REQUEST['name'] . Every key we write each request we can avoid it writing follwoing code .

foreach ($_REQUEST as $key =>$value) {
$$key = addslashes(trim($value));
}

Display database table record with foreach loop :

$link = mysql_connect ("localhost","root","") or die (" Could not connect to localhost");

$conn= mysql_select_db("connect0844", $link) or die (" Could not connect to database connect0844") ;

$sql = "SELECT * FROM address where id='1'";

$result = mysql_query($sql) or trigger_error("SQL", E_USER_ERROR);

$list = mysql_fetch_assoc($result);

foreach ($list as $key => $value) {
$$key = addslashes(trim($value));
}


echo "$name\n" ;
echo "$email\n" ;
echo "$address" ;

No comments:

Post a Comment