Techinfoworld.com

Spreading technical and open source informations to world !

PHP,MySQL - CLASS BASED DATABASE CONNECTION

An Object Oriented approach to establlishing and managing database connections.
Here I used MySQL Database for Integrating with PHP Scripting (because the famouse combination).
The class “Profile” is a wrapper for developing our PHP database connection.

class Profiles{
var $error=”;
var $link;
// Establish Database connection while creating Objects
function __construct($dbhost,$dbuser,$dbpass,$dbase){
$this->link=mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($dbase);
}
function addData($pid,$uname,$dob) {
global $link;
// Insert data to database
$sql = ‘INSERT INTO `profile` (`pid`, `uname`, `dob`) VALUES (\”.$pid.’\', \”.$uname.’\', \”.$dob.’\');’;
// Execute SQL query
$res=mysql_query($sql);
// Check for Erorrs
if(!$res){
$this->error.=”Profile insertion failed.”.mysql_error();
return 0;
}
if(mysql_affected_rows()<1){
$this->error.=”No rows affected”;
return 0;
}
return 1;
}

// Call destructor for de-link MySQL connection at Object
function __destructor(){
mysql_close($this->link);
}
}

In your coding create an Object for the class Profile with parameters of,

  • $dbname - Database host name (example, localhost, yourdomain.com, sub.mydomain.com .. ).
  • $dbuser - Database Username ( the user already exists with required privileges).
  • $dbpass - Password for that username.
  • $dbase - Which is “Database name” to be used for importing tables,passing queries, calling procedures.

The class methods “__construct” and “__destructor” use in PHP:

I used a default constructor for establishing database connection from the arguments received while creating objects,

$pro_obj=new Profile(”localhost”,”myuser”,”mypass”,”db_users”);

- The above oneline code creates an object “$pro_obj” for the class “Profile” at the same time it established a default connection with MySQL Database.

- The method “AddData” in class Profile, used for passing queries to database (db_users).

- At last line the destructor method __destructor delinks the established connection while the object($pro_obj) removed from memory.

DETECTING MOBILE PHONES USING PHP CODE

PHP CODE TO DETECT MOBILE BROWSERS

Detecting Handheld devices like , mobile phones, PDA, WAP enabled devices, WAP enabled Browsers is possible with PHP Scripting.

The code that detects mobile devices:

 $wapprofile=$_SERVER['HTTP_X_WAP_PROFILE']; // mobile profile
 $httpprofile=$_SERVER['HTTP_PROFILE']; // http profile
 $browser=$_SERVER['HTTP_USER_AGENT']; // user-agent info

 // — MOBILE DETECTION — //
 $wml=0;
 $xhtml=0;
 if(preg_match(’/(midp|wap|up.browser|up.link|mmp|symbian|smartphone|phone)/i’,strtolower($browser))) {
  $wml++;
 }
 if((strpos(strtolower($_SERVER['HTTP_ACCEPT']),’application/vnd.wap.xhtml+xml’)>0)) {
  $xhtml++;
  $wml++;
 }
 if(strpos(strtolower($_SERVER['HTTP_ACCEPT']),’xhtml+xml’)>0) {
  $xhtml++;
 }
 if(isset($wapprofile) or isset($httpprofile)) {
  $wml++;
 }
 $mobile_ua = strtolower(substr($browser,0,4));
 $mobile_agents = array(
  ’doco’,'eric’,'hipt’,'inno’,'w3c ‘,’audi’,'avan’,'benq’,'bird’,'blac’,
  ’blaz’,'brew’,'cell’,'oper’,'palm’,'pana’,'pant’,'cldc’,'cmd-’,'dang’,
  ’ipaq’,'java’,'jigs’,'kddi’,'keji’,'leno’,'lg-c’,'lg-d’,'lg-g’,'lge-’,
  ’maui’,'maxo’,'midp’,'mits’,'mmef’,'mobi’,'mot-’,'moto’,'mwbp’,'nec-’,
  ’newt’,'noki’,'phil’,'play’,'port’,'prox’,'acs-’,'alav’,'alca’,'amoi’,
  ’qwap’,’sage’,’sams’,’sany’,’sch-’,’sec-’,’send’,’seri’,’sgh-’,’shar’,
  ’sie-’,’siem’,’smal’,’smar’,’sony’,’sph-’,’symb’,'t-mo’,'teli’,'tim-’,
  ’tosh’,'tsm-’,'upg1′,’upsi’,'vk-v’,'voda’,'wap-’,'wapa’,'wapi’,'wapp’,
  ’wapr’,'xda-’,'webc’,'winw’,'winw’,'xda’);

 if(in_array($mobile_ua,$mobile_agents)){
  $wml++;
 }
 if(strpos(strtolower($_SERVER['ALL_HTTP']),’OperaMini’)>0) {
  $xhtml++;
  $wml++;
 }
 if(strpos(strtolower($browser),’windows’)>0) {
  $xhtml++;
 }

 if($xhtml && $wml)
  echo “Supports both XHTML and WML codes - Mobie with XHTML”;
 else if($wml && ($xhtml==0))
  echo “Supports WML only - truely mobile”;
 else if(($wml==0) && $xhtml)
  echo “Supports XHTML (ex: Desktop Browser) - may be mobile”;
 else
  echo “Not a Mobile”;

- You should replace the appropriate lines for Redirecting to web pages to a particular devices.