Friday, December 19, 2014

Uses of Indexes in mysql

Uses of Indexes in mysql:

A database index is a data structure that improves the speed of operations in a table. Indexes can be created using one or more columns, providing the basis for both rapid random lookups and efficient ordering of access to records.

While creating index, it should be considered that what are the columns which will be used to make SQL queries and create one or more indexes on those columns.

INSERT and UPDATE statements take more time on tables having indexes where as SELECT statements become fast on those tables. The reason is that while doing insert or update, database need to insert or update index values as well.

For adding index in table :

CREATE UNIQUE INDEX index_name
ON table_name ( column1, column2,...);

You can use one or more columns to create an index. For example, we can create an index on tutorials_tbl using tutorial_author.

CREATE UNIQUE INDEX AUTHOR_INDEX
ON tutorials_tbl (tutorial_author)

You can create a simple index on a table. Just omit UNIQUE keyword from the query to create simple index. Simple index allows duplicate values in a table.

If you want to index the values in a column in descending order, you can add the reserved word DESC after the column name.

mysql> CREATE UNIQUE INDEX AUTHOR_INDEX
ON tutorials_tbl (tutorial_author DESC)


Thanks

Friday, December 5, 2014

Completely Login your site using Facebook, Here Backend - Laravel(php) & Frontend - Ionic(for mobile app)

Completely Login through Facebook:

Hi all, Today i am here for writing an article which is used for login your mobile app through Facebook.

Here, i am doing this using APIs. So, my backend is Laravel Framework(PHP)  & my frontend is Ionic Framework(mobile app).

Backend:

Just write down these function after your login function in your model .

    public function fbLogin(){
        $input = Input::all();          //getting all data(name,email,fb_id) from frontend
        $email = $input['email'];
        $fbId = $input['fb_id'];
        $firstName = $input['first_name'];
        $lastName = $input['last_name'];

        $user = DB::table('users')
            ->where('username', $email)
            ->orWhere('fb_id', $fbId)
            ->get();
        $noOfRow = count($user);   //getting data from table w.r.t. email




//in case where fb_id & email are exists in DB & same as getting data  row will show 1, in case fb_id $email exists in DB but not both are same then case =2, in case when both does not exists case = 0
      if($noOfRow==1){
            DB::table('users')->where('username', $email)->update(array('fb_id' => $fbId));      
            $userID = $user[0]->id;
        }elseif($noOfRow==2){
            foreach($user as $v){
                if($v->fb_id==$fbId){
                    $userID = $v->id;
                }
            }
        }else{
            if($email){
               return $this->doLoginforFB($email,$fbId, $firstName,$lastName );
            }else{
                $timeStamp = md5(microtime());
                $dummyEmail = $timeStamp.'@dummy.com'; //creating dummy email
                return $this->doLoginforFB($dummyEmail,$fbId, $firstName,$lastName );
            }
        }
         return $this->getUsersDetailsById($userID);
    }

    public function doLoginforFB($email,$fbId, $firstName,$lastName ){
        $password = mt_rand(10000000, 99999999);  //creating a dummy password
        $hashPass = Hash::make($password);

        $newUser = new User();
        $newUser->username = $email;
        $newUser->password = $hashPass;
        $newUser->fb_id = $fbId;
        $newUser->save();

        $this->RegisterStep($newUser->id);

        DB::table('users_details')->insert(
            array('firstname' => $firstName, 'lastname' => $lastName, 'user_id'=>$newUser->id)
        );

       
        $input['username'] = $email;
        $input['password'] = $password;

        Input::replace($input);

        $request = Request::create('oauth/login', 'POST');  //laravel way for passing parms in doLogin function or you can call here your Login function
        $response = Route::dispatch($request);
        return $response;
    }













Frontend:

Accordingly using this i have created my Frontend.

Just follow its steps and get all the details of any user like email, fb_id , name and send it to your backend.

After that make a build and check it out if its working

Thanks
Ashish Ginotra