PHP Select语句包含条件

时间:2018-04-01 14:09:24

标签: php android retrofit

我是Android和PHP的新手,当我们知道只有单个数据会返回时如何从数据库获取数据,我已经有我的PHP代码和android项目,但在我的php返回使用数组我想只返回一个数据不在数组中,因为我知道只会选择一个数据,我想使用改造在我的android项目中获取值,这是我的代码

的search.php

<?php  require_once('config.php');
if($_SERVER['REQUEST_METHOD']=='POST'){
$email=$_POST['email'];
$sql="SELECT * FROM pelanggan where email LIKE '%$email%' ";
$res=mysqli_query($con,$sql);
$result=array();
while($row=mysqli_fetch_array($res)){
    array_push($result,array('id_pel'=>$row[0],'id_role'=>$row[1],'email'=>$row[2],'nama'=>$row[3],'no_identitas'=>$row[4],'telp'=>$row[5],'alamat'=>$row[6]));
}
echo json_encode(array("value"=>1,"result"=>$result));
mysqli_close($con);}?>

Register.java

    public Pelanggan getPelangganByEmail(String email){
    List<Pelanggan> myList=new ArrayList<>();
    Retrofit retrofit=new Retrofit.Builder()
            .baseUrl(URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    RegisterAPI api=retrofit.create(RegisterAPI.class);
    Call<Value> call=api.searchPelanggan(email);

    call.enqueue(new Callback<Value>() {
        @Override
        public void onResponse(Call<Value> call, Response<Value> response) {
            //what I should do in this statement
        }

        @Override
        public void onFailure(Call<Value> call, Throwable t) {

        }
    });
    return null;
}

1 个答案:

答案 0 :(得分:0)

最好只在JSON的正文中创建REST服务器而不是响应HTTP。但对于您的情况,要发送JSON的单个对象而不是数组。编码PHP的简单object而不是编码array

<?php  require_once('config.php');
if($_SERVER['REQUEST_METHOD']=='POST'){
$email=$_POST['email'];
$sql="SELECT * FROM pelanggan where email LIKE '%$email%' ";
$res=mysqli_query($con,$sql);
$row=mysqli_fetch_array($res)

$myObj->id_pel = $row[0];
$myObj->id_role = $row[1];
$myObj->email = $row[2];
.................

$myJSON = json_encode($myObj);
echo $myJSON;

mysqli_close($con);}?>