PHP Developing Guide

Other

Even if nowadays most of web php application are written using various frameworks, or platforms for a beginner i consider that first of all he should learn to develop application in pure PHP code. For those who continue to develop, web application using pure PHP code i present below some usefull functions/ piece of code used in developing php application from scratch. 1. Function that connect to db.

private function connect_db(){
    if ($dbc=@mysql_connect('host','username','password'))      // make connection
    {
        if (!@mysql_selectdb('dbname')) // select db
        {
            die ('I can not select db: <b>'.mysql_error().'</b>'); 
        }
    } else{ 
       die('Connection fail: <b>'.mysql_error().'</b>'); 
    } 
    return $dbc; 
}

 

2. Function to send email in html format:

 

public function send_mail($subject, $content){
     $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
     $headers .= 'From: MySelf <myself@site.com>' . "\r\n";
     @mail('admin@site.com', $subject, nl2br($content), $headers);       
}

3. Function to get user from db:

public function get_user($id){
  $this-&gt;connect_db();    
  $rez=null;
  $query= "SELECT * FROM users_table WHERE id_user = '$id'";
  $result=mysql_query($query);
  if(mysql_num_rows($result)==1)
  {
   $rez=mysql_fetch_object($result);   
  }
  return $rez; 
}

Add comment
 
 
 

Comments