Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a PHP file below
PHP
<pre><?php
require dirname(__FILE__).'/../DbConnection/conn.php';

if($conn){
    $sqlStr = "SELECT
    YEAR(OrderBill.BillingDate) AS year,
    SUM(MultiPay.Amount) AS growth_rate
    FROM Rst_TblMultiPayModes MultiPay
    LEFT OUTER JOIN Rst_TblOrderBills OrderBill
    ON MultiPay.OrderBillCode = OrderBill.OrderBillCode
    WHERE OrderBill.isBillCleared=1 AND OrderBill.isMerged=0
    AND MultiPay.PaymentMode IN(1,2,3,4,6,7)
    $_GET['mselectperiod']

    GROUP BY
    YEAR(OrderBill.BillingDate)";

    $sqlResult = mysqli_query($conn,$sqlStr);
    $response = array();

    while($row = mysqli_fetch_assoc($sqlResult)){
        array_push($response,$row);
    }
    
    echo json_encode($response);
    mysqli_close($conn);
}else{
    echo "Host Connection Error";
}
?>

This PHP file works fine when I execute it in my browser (http://localhost/hotelwiseapp/Reports/salesgrowthratechartyear.php?mselectperiod=AND YEAR(OrderBill.BillingDate)=2019 AND YEAR(OrderBill.BillingDate)=2022)
and resulting as follows

"[{"year":"2019","growth_rate":"908650"},{"year":"2020","growth_rate":"16957700"},{"year":"2021","growth_rate":"27837121"}]"

Now when it comes to Android, I use Retrofit library as follows by creating an interface

Java
<pre>public interface ApiInterface {
    @GET("{PHP_QRY_FILE_NAME}")
    Call<List<Growth>> getGrowthInfo(@Path(value="PHP_QRY_FILE_NAME",encoded = true) String mPhpQryFileName);

}

Then I call as follows
Java
call = ChartApiClient.getApiClient().create(ApiInterface.class).getGrowthInfo("salesgrowthratechartyear.php);

The Above Also Works Fine But Only when I Remove $_GET['mselectperiod'] In The PHP File. The Problem Comes When I want To Use $_GET['mselectperiod'] In That Php File and then modifying My Interface with a @Query Annotation As Follows
Java
<pre>public interface ApiInterface {
    @GET("{PHP_QRY_FILE_NAME}")
    Call<List<Growth>> getGrowthInfo(@Path(value="PHP_QRY_FILE_NAME",encoded = true) String mPhpQryFileName, @Query("mselectperiod") String mWhereClause);

}

Then I call as follows
Java
call = ChartApiClient.getApiClient().create(ApiInterface.class).getGrowthInfo("salesgrowthratechartyear.php",SalesGrowthChart.mFinalSelectPeriodQry);

Note That 'SalesGrowthChart.mFinalSelectPeriodQry' is a variable That Receives a Changing String values as below

"AND YEAR(BillingDate)>=2019 AND YEAR(BillingDate)<=2022"

Nothing happens. What am I missing?

What I have tried:

Passing string parameters to a PHP file Using the @Query Annotation
Posted
Updated 23-Dec-21 0:05am

1 solution

Edited the Interface to
Java
<pre>public interface ApiInterface {
@GET("{PHP_QRY_FILE_NAME}")
Call<List<Growth>> getGrowthInfo(@Path(value="PHP_QRY_FILE_NAME",encoded = true) String mPhpQryFileName, @Query(value="mselectperiod",encoded = true) String mWhereClause);

For the @Query annotation, I omitted the 'Value=' and also It was supposed to be encoded to true(,encoded = true)
Then in my PHP file I edited as follows for the $_GET
HTML
<pre><?php
require dirname(__FILE__).'/../DbConnection/conn.php';

$mselectperiod = $_GET["mselectperiod"];

if($conn){
    $sqlStr = "SELECT
    YEAR(OrderBill.BillingDate) AS year,
    SUM(MultiPay.Amount) AS growth_rate
    FROM Rst_TblMultiPayModes MultiPay
    LEFT OUTER JOIN Rst_TblOrderBills OrderBill
    ON MultiPay.OrderBillCode = OrderBill.OrderBillCode
    WHERE OrderBill.isBillCleared=1 AND OrderBill.isMerged=0
    AND MultiPay.PaymentMode IN(1,2,3,4,6,7)
    $mselectperiod

    GROUP BY
    YEAR(OrderBill.BillingDate)";

    $sqlResult = mysqli_query($conn,$sqlStr);
    $response = array();

    while($row = mysqli_fetch_assoc($sqlResult)){
        array_push($response,$row);
    }
    
    echo json_encode($response);
    mysqli_close($conn);
}else{
    echo "Host Connection Error";
}
?>

And it worked fine . . .
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900