【问题标题】:PHP Select statement with conditions带条件的 PHP Select 语句
【发布时间】:2018-09-10 22:32:52
【问题描述】:

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

搜索.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;
}

【问题讨论】:

  • 这是 PHP 问题还是 Java 问题?听起来您在询问如何处理 java.util.php 中的 PHP 返回。您的 PHP 对 SQL 注入开放。

标签: php android retrofit


【解决方案1】:

创建 REST 服务器比在 HTTP 的正文中响应 JSON 更好。但是对于您的情况,发送 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);}?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-09
    • 1970-01-01
    • 1970-01-01
    • 2017-03-04
    • 2016-06-03
    • 2015-06-01
    相关资源
    最近更新 更多