API (PHP,MYSQL)

API (PHP,MYSQL)

  • an API (Application Programming Interface) for Android application development refers to a set of rules, protocols, and tools that allow different software applications to communicate with each other.
  • In the context of Android development, APIs are essential for accessing various functionalities of the Android operating system, such as interacting with hardware components, accessing system services, and integrating with other apps.
  • Developers need to understand these APIs, follow their guidelines, and use them appropriately to build robust and efficient Android applications. Additionally, APIs are continuously updated and improved, so staying updated with the latest changes and practices is crucial for Android developers.

Explanation:

  • As we know that PHP code written in <?php ... ?> tag
  • mysqli_connect() function make connection with our database (available in server)
    • it takes four arguments as bellow:
        1. host
        1. user of database
        1. password of database
        1. name of database
    • this information can be found on server or if you have store some where while creating database
    • it returns true / (1) if connection with database is established or false / error / (0) when connection is not established (when wrong credential are given to connect with database ).
    •  
  • $_POST['KEY'] it used to get values of asked key from API that was sent in form of map into API-(URL)
 
  • mysqli_query() are used to execute/fire query on database
    • It takes two arguments
        1. connection object with database
        1. query that need to perform on database
      • it return true / (1) if query executed or error if some problem occurs in connection object or query.
      • it return mysqli_result object for select query .
 
  • mysqli_num_rows(result object) return / count total numbers of rows from result object provided by executing query
 
  • base64_decode() are used to decode encoded file that was converted in to base64 format and returns that decoded format
 
  • file_put_contents is used to store file content to provided path in server
    • it takes two argument
      1. path/file_name.extension
      1. decoded file
       
  • Json_encode(array) is used to provide result in JSON format to fetch and work on it
 

insert.php

<?php // $con = mysqli_connect("hostname","username","password","Databasename") $con = mysqli_connect("localhost","id21598829_amdavadwalauser","Devansh@123","id21598829_devanshdatabase") ; $temp = array(); if($con) { $temp['Connection'] = 1; }else { $temp['Connection'] = 0; } // variable = values to be get from keys provided in [] bracket $_name = $_POST['name']; $_email = $_POST['email']; $_number = $_POST['number']; $_password = $_POST['password']; $_imgpath = $_POST['imgpath']; // check if same data already exists or not in database $select_qry = "select * from UserData where EMAIL = '$_email' OR NUMBER = '$_number' "; $sql = mysqli_query($con , $select_qry); $rr = mysqli_num_rows($sql); if ($rr == 0) { // decode endoded data got in string/base64 format $realimage = base64_decode($_imgpath); //create path, name , file extension for decoded file to store $imagename = "Userdp/"."$_name".rand(10000,99999).".jpg"; // put / store decoded file with generated name to provided path file_put_contents($imagename, $realimage); $insert_qry = "insert into UserData (NAME , EMAIL , NUMBER , PASSWORD , IMAGEPATH) values ('$_name','$_email','$_number','$_password','$imagename')"; $sql = mysqli_query($con , $insert_qry); if ($sql) { $temp['result'] = 1; }else { $temp['result'] = 0; } echo json_encode($temp); } else { $temp['result'] = 2; echo json_encode($temp); } ?>