Username Validation in PHP

// Username Validation //
if( isset($_POST[‘username’]) && !empty($_POST[‘username’]) && (preg_match(“/^[A-Z][a-zA-Z -]+$/”, $_POST[“username”]) === 0))
{
$errName = ‘<p class=”errText”>Name must be from letters, dashes, spaces and must not start with dash</p>';
echo $errName;
$errorValue = 1;
}

mysql_real_escape_string()

mysql_real_escape_string() :

The mysql_real_escape_string()  function escapes special characters in a string for use in an SQL statement. The following characters are affected:

  • \x00
  • \n
  • \r
  • \
  • \x1a

This function returns the escaped string on success, or FALSE on failure.

 Syntax:
      mysql_real_escape_string(string,connection)

Simple method to query server – HTTP POST

#define DEBUG_URL @”http://yourServer.com/myPHPfile.php?”


-(NSString*)  queryOnlineDB:(NSString*)request

{

NSData *postData = [request dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];

NSMutableURLRequest *requestURL = [[[NSMutableURLRequest alloc] init] autorelease];

NSURL *url=[NSURL URLWithString:DEBUG_URL];

[requestURL setURL:url];

[requestURL setHTTPMethod:@"POST"];

[requestURL setValue:postLength forHTTPHeaderField:@"Content-Length"];

[requestURL setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

[requestURL setHTTPBody:postData];

NSError *error;

NSURLResponse *response;

NSData *urlData=[NSURLConnection sendSynchronousRequest:requestURL returningResponse:&response error:&error];

return [[NSString alloc]initWithData:urlData encoding:NSASCIIStringEncoding];

}

Nested Drop down in PHP

$sql = “SELECT childId, childName, parentId, parentName FROM child LEFT JOIN parent ON child.parentId = parent.parentId ORDER BY parentID, childName”;

$result = mysql_query($sql);

$currentParent = “”;

print “<select>”;

while($row = mysql_fetch_assoc($result)){

if($currentParent != $row[“parentID”]){

if($currentParent != “”){

print “</optgroup>”;

}

print “<optgroup label=\”.$row[“parentName”].”\”>”;

$currentParent = $row[“parentName”];

}

print “<option value=\””.$row[“childID”].”\”>”.$row[“childName”].”</option>\n”;

}

print “</optgroup>”

print “</select>”;

Memcached in php

Disk access is slow. Network access is slow. Databases typically use both.

Memory is fast. Using a local cache avoids the overhead of network and disk access. Combine these truths and you get memcached, a “distributed memory object caching system” originally developed for the Perl-based blogging platform LiveJournal.

If your application isn’t distributed across multiple servers, you probably don’t need memcached. Simpler caching approaches — serializing data and storing it in a temporary file, for example — can eliminate a lot of redundant work on each request. In fact, this is the sort of low-hanging fruit we consider when helping our clients tune their apps.

One of the easiest and most universal ways to cache data in memory is to use the shared memory helpers in APC, a caching system originally developed by our colleague George Schlossnagle. Consider the following example:

<?php
$feed = apc_fetch('news');


if ($feed === FALSE) {

$feed = file_get_contents('http://example.org/news.xml');

// Store this data in shared memory for five minutes.

apc_store('news', $feed, 300);

}

// Do something with $feed.

?>

With this type of caching, you don’t have to wait on a remote server to send the feed data for every request. Some latency is incurred — up to five minutes in this example — but this can be adjusted to as close to real time as your app requires.

PHP Random collection of element from an array

We can randomly collect one or more element from an array by using array_rand() function in PHP. We can specify the number of random elements required by specifying an optional parameter in side the function.

$value= array("Rabin","Reid","Cris","KVJ","John");

$rand_keys=array_rand($value,2);
echo "First random element = ".$value[$rand_keys[0]];
echo "<br>Second random element = ".$value[$rand_keys[1]];