My Main Blog

Saturday, 22 November 2014

How to enable debug on in Laravel

How to enable debug on in Laravel

While working with laravel Framework you could face this error "Whoops, looks like something went wrong."
To overcome this error first you need to see what is the exact error in your code. to show exact error you need to set debug true in "app.php" file.

Follow below steps:
  1. Open "\app\config\app.php" file.
  2. Set " 'debug' => false" to 'debug' => true
 Now you can the exact error and fix the error.



Monday, 17 November 2014

Url rewriting in Php

Very simple Url rewriting in Php

Suppose you have a website and you are operating it with query string as below:
http://www.yoursite.com/computer.php?company=asus&name=s56c

you are using two query string "company" and "name". In "company" you are passing "asus" on the other-hand in "name" you are passing "s56c". So the question is that how to convert

"http://www.yoursite.com/computer.php?company=asus&name=s56c"
                                   TO
"http://www.yoursite.com/computer/asus/s56c.html"

The url "http://www.yoursite.com/computer/asus/s56c.html" is not SEO friendly. so how to make it SEO friendly?

It is very simple. for this you need to create ".htaccess" in the root folder where your site's  files existing. paste listed below 3 lines in ".htaccess" file.


Options +FollowSymLinks
RewriteEngine on
RewriteRule computer/(.*)/(.*)\.html computer.php?company=$1&name=$2



And this is the content of your "computer.php" file

<?php
$company=$_GET['company'];
$name=$_GET['name'];
echo $company."--".$name;
?>


open the computer.php as "http://www.yoursite.com/computer/asus/s56c.html"


Access mysql data using mysqli_connect in PHP Class


Access mysql data using mysqli_connect in PHP with mysqli_query


Access mysql record by Object oriented method using class and object in PHP. Here is the class name is "DBClient" and it's method name is "exeQueryi()", that is return record from mysql table name "user_login". The __construct() method automatically called when we create any object of class "DBClient". Here I am using mysqli_connect,mysqli_query,mysqli_num_rows and mysqli_fetch_array.


<?php
class DBClient {
var $host,$db,$user,$pass;
var $linki,$isDBConnect;

function __construct() {
    $this->host = "localhost";
    $this->db = "mydb";
    $this->user = "root";
    $this->pass = "";
    try {
        $this->linki = mysqli_connect($this->host, $this->user, $this->pass,$this->db );
        if (!is_resource($this->linki)) {
            $this->isDBConnect=false;
        } else {
            $this->isDBConnect=true;
        }
    } catch(Exception $ee) {
        $this->linki=false;
           $this->isDBConnect=false;
    }
}

public function exeQueryi($query) {
    try {
        $result = mysqli_query( $this->linki,$query);
        return $result;
    } catch(Exception $ee) {
        return (FALSE);
    }
}
}
?>

<?php
$user_login="admin";
$user_pass="admin";
$sql=sprintf("SELECT *FROM user_info WHERE user_login = '%s' and user_pass='%s'", $user_login,$user_pass);
$db=new DBClient();
$rsLogin=$db->exeQueryi($sql);
$numRows = mysqli_num_rows($rsLogin);
if($numRows>0) {
        $rsLoginRow=mysqli_fetch_array($rsLogin);
        $user_login= $rsLoginRow['user_login'];
        $user_pass=$rsLoginRow['user_pass'];
echo "login found";
} else {
echo "login not found";
}
?>



Wednesday, 5 November 2014

incorrect date time in PHP

incorrect date time in PHP

set timezone in PHP

You could be face a accurate Indian date time problem in PHP, because web server located to USA or any other country. that's why date and time which you are looking is wrong. but you can set it. so after set it you can get the correct indian time. you only need to set timezome as listed below:

date_default_timezone_set('Asia/Calcutta');

Export to excel in PHP

Export to excel in PHP

If you have a data and want to generate excel file of that data. then call this listed below function in php.



public function exportToExcelUtil($filename,$data) {
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=$filename");
header("Pragma: no-cache");
header("Expires: 0");
echo  $data;
}

Validate email in PHP

Validate email in PHP

Here is the simple php function by this you can validate email which entered by any user.


public function checkEmail($mailID) {
$pat = "/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-.]+\.[a-zA-Z. ]{2,6}$/";
if(preg_match($pat,$mailID)) {
return true;
} else {
return false;
}
}

Tuesday, 4 November 2014

Access mysql data using Class in PHP

Access mysql data using Class in PHP using mysql_connect and use mysql_select_db and mysql_query



Access mysql record by Object oriented method using class and object in PHP. Here is the class name is "DBClient" and it's method name is "getDistrict", that is show records from mysql table name "district_names". The __construct() method automatically called when we create any object of class "DBClient".


<?php
class DBClient{
var $host;
var $db ;
var $user;
var $pass;
var $link,$isDBConnect;
var $isError,$errorValue;
function __construct() {
$this->host = "localhost";
$this->db = "dbname";
$this->user = "root"; 
$this->pass = ""; 
try {
$this->link = mysql_connect($this->host, $this->user, $this->pass);
if (!is_resource($this->link)) {
$this->isDBConnect=false;
}
else {
    mysql_select_db($this->db,$this->link);
$this->isDBConnect=true;
}
}
catch(Exception $ee){
$this->link=false;
   $this->isDBConnect=false;
}
}
public function exeQuery($query) {
try {
$result = mysql_query($query, $this->link);
return $result;
}
catch(Exception $ee){
return (FALSE);
}
}
public function getDistrict() {
 $res=false;
 try {
  $sSql="SELECT * FROM district_names";
  $res=$this->exeQuery($sSql);
  $rows = mysql_num_rows($res);
  if($rows>0) {
  } else {
$res=false;
  } 
 } catch(Exception $ee){
  $res=false; 
 } 
 return $res; 
}
}

$dd=new DBClient();
$cntDis=$dd->getDistrict();
if($cntDis) {
while($cntRow=mysql_fetch_array($cntDis)) {
$district_id= $cntRow['district_id'];
$districtname=$cntRow['districtname'];
echo $district_id.'====='.$districtname;
}
 }
?>