For smart Primates & ROBOTS (oh and ALIENS ).

Thursday, March 12, 2015

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";
}
?>
Share:

Show correct date time 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:

<?php
date_default_timezone_set('Asia/Calcutta');
?>
Share:

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;
}
Share:

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;
}
}
Share:

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;
}
 }
?>
Share:

Latitude Longitude by address in php

You can find very easily Latitude and Longitude by given address with combination of Google map api and php.
You can easily implement in CodeIgniter,Laravel , CakePHP or any other framewok.

<?php
    $address = 'Sector 37,Noida,India';
    $geocode = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode($address) . '&sensor=true');
    $geocode  = json_decode($geocode );
    echo 'Latitude:' . $geocode ->results[0]->geometry->location->lat;
    echo 'Longitude:' . $geocode ->results[0]->geometry->location->lng;

?>
Share:

Remove special character in php

You have a website and in that website you have lot of form for user. user fill many data via that forms. some time user feed special characters that are unreadable. so how to remove that special characters before inserting in data base? here is the few lines of code that will do it.

<?php
$input = "Fóø Bår this is the php form data ?A?B?C?D //\\?......!@#$%^&*()_+"; // input text
$output = iconv("utf-8", "ascii//TRANSLIT//IGNORE", $input);
$output =  preg_replace("/^'|[^A-Za-z0-9\s-]|'$/", '', $output); // Remove utf-8 special characters except blank spaces
echo $output; // Results
?>
Share:

Cell align top in dataGridView in c#

Use one line code as below:

dataGridView1.Columns["Question"].DefaultCellStyle.Alignment=DataGridViewContentAlignment.TopLeft;
Share:

Get Volume Serial Number of Drive in C#

Listed below the code use in your project to get Volume serial number

//let's show C drive's volume serial number
String d = "c";
try {
    System.Management.ManagementObject dsk1 = new System.Management.ManagementObject(@"win32_logicaldisk.deviceid=""" + d + @":""");
    dsk1.Get();
    string s1 = dsk1["VolumeSerialNumber"].ToString();
    MessageBox.Show(s1);
}
catch (Exception ee)
{
    MessageBox.Show(ee.ToString());

}
Share:

SEND EMAIL FROM WINFORM

Some times you want to open microsoft outlook from microsoft winform and then type the mail message and send it. use below one line of code.


System.Diagnostics.Process.Start("mailto:XYZ@XYZ.COM");
Share:

Ads Inside Post

Powered by Blogger.

Archive