PHP Restful API Framework SLIM to Create REST API – Android Coding by DMTechnolab

Simplified Coding


<? php

class DbOperation

{{

// database connection link

Private With $;

// class constructor

The ceremony __conversation())

{{

// Getting the DbConnect.php file

require once dirname()__FILE__) . ‘/DbConnect.php’;

// Create a DbConnect object to connect to the database

$ Db = New DbConnect());

// starting our connection link of this class

// By calling method connect of DbConnect class

This $->With = $ Db->connect());

}

// method will create a new student

public The ceremony Creator()$ Name,$ Username,$ Pass){{

// First we will check whether the student is already registered or not

If ()!This $->isStudentExists()$ Username)) {{

// encrypt password

$ Password = md5()$ Pass);

// generate an API key

$ Apology = This $->GenerApiKey());

// a statement crating

$ stmt = This $->With->ready()“INSERT INTO students (name, username, password, api_key) value (?,?,?)?”);

// binding parameters

$ stmt->bind_param()“ssss”, $ Name, $ Username, $ Password, $ Apology);

// law violation

$ Result = $ stmt->Executed());

// Close the statue

$ stmt->Close());

// If the law is executed successfully

If ()$ Result) {{

// Returning 0 means the student is successfully created

The return 0;

} other {{

// Returning 1 means failed to create a student

The return Have 1;

}

} other {{

// returning 2 means the user already exists in the database

The return 2;

}

}

// method for student login

public The ceremony Student lojin()$ Username,$ Pass){{

// Generating Password Hash

$ Password = md5()$ Pass);

// create query

$ stmt = This $->With->ready()“Select * from students where username =? And password =?”);

// binding parameters

$ stmt->bind_param()“ss”,$ Username,$ Password);

// executing the query

$ stmt->Executed());

// get results

$ stmt->store_result());

// get results

$ num_rows = $ stmt->num_row;

// Close the statue

$ stmt->Close());

// If the result value is greater than 0 then the user is found in the database with the given username and password

// So the truth is returning

The return $ num_rows>0;

}

// This method will return the details of the student

public The ceremony Get firm()$ Username){{

$ stmt = This $->With->ready()“Select * from students where username =?”);

$ stmt->bind_param()“s”,$ Username);

$ stmt->Executed());

// Getting the student result array

$ Student = $ stmt->get_result())->fetch_assoc());

$ stmt->Close());

// return to student

The return $ Student;

}

// check if a student already exists

Private The ceremony isStudentExists()$ Username) {{

$ stmt = This $->With->ready()“Select id from students where username =?”);

$ stmt->bind_param()“s”, $ Username);

$ stmt->Executed());

$ stmt->store_result());

$ num_rows = $ stmt->num_row;

$ stmt->Close());

The return $ num_rows > 0;

}

// method to get the assignment

Private The ceremony getAssignments()$ Id){{

$ stmt = This $->With->ready()“Select * students_id = in FFROM assignment?”);

$ stmt->bind_param()“I”,$ Id);

$ stmt->Executed());

$ Assignment = $ stmt->get_result())->fetch_assoc());

The return $ Assignment;

}

/ *

* User checking methods are valid or not using API key

* I will not write comments for every method because each method does the same thing

* * /

public The ceremony isValidStudent()$ Ap_k) {{

// make a statement

$ stmt = This $->With->ready()“Where is the select id from students api_key =?”);

// binding parameter to statement with this

// Question marks will be replaced with actual values

$ stmt->bind_param()“s”, $ Ap_k);

// execute the statement

$ stmt->Executed());

// Storing the result

$ stmt->store_result());

// getting rows from database

// As API Key is always unique so we will either get one row or no row

$ num_rows = $ stmt->num_row;

// Close the statue

$ stmt->Close());

// Returning true if the received row is greater than 0 means the user is valid

The return $ num_rows > 0;

}

// This method will generate a unique API key

Private The ceremony GenerApiKey()){{

The return md5()Bodiless()Rand()), True));

}

}



Source link

Be the first to comment

Leave a Reply

Your email address will not be published.


*