Ads Inside Post

Search in This Blog

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