php - 如何为通过 PHP 发送到 android 的 json 数据设置 if 条件

标签 php android mysql json

我有一个 PHP 文件,它从 MySQL 数据库获取数据并将其作为 json 数组发送到 android。至于现在,所有行都发送到 android 应用程序。我只想发送 approved 值为 1 的行。我该怎么做?我试过了

while ($row = mysql_fetch_array($result, MYSQL_ASSOC) && ($row['approved']==1))

但它不起作用。这是我的 PHP 文件

<?php

//Create Database connection

$db = mysql_connect("localhost","shareity","sh@r31tymysql");

if (!$db) {

    die('Could not connect to db: ' . mysql_error());

}


//Select the Database

mysql_select_db("shareity",$db);



//Replace * in the query with the column names.

$result = mysql_query("select * from event", $db); 



//Create an array

$json_response = array();



while ($row = mysql_fetch_array($result, MYSQL_ASSOC) ) {

    $row_array['eid'] = $row['eid'];

    $row_array['ename'] = $row['ename'];

    $row_array['etype'] = $row['etype'];

    $row_array['edesc'] = $row['edesc'];

    $row_array['esdate'] = $row['esdate'];

    $row_array['eedate'] = $row['eedate'];

    $row_array['estime'] = $row['estime'];

    $row_array['eetime'] = $row['eetime'];

    $row_array['location'] = $row['location'];

    $row_array['created_at'] = $row['created_at'];

    $row_array['edited_at'] = $row['edited_at'];

    $row_array['createdby'] = $row['createdby'];

    $row_array['approved'] = $row['approved'];




    //push the values in the array

    array_push($json_response,$row_array);

}

//echo json_encode($json_response);
echo json_encode(array('events'=>$json_response));


//Close the database connection

fclose($db);

?>

最佳答案

就在您的 while 循环之后,添加一个 if 并在该行不符合您的条件时继续:

 while ($row = mysql_fetch_array($result, MYSQL_ASSOC) ) {
      if ($row['approved'] != 1)
           continue; // We don't want to have any rows that are not approved.
      // code...
 }

或者,您可以选择限制您的 SQL 查询,以便只检索批准的行。

 $result = mysql_query("select * from event where `approved` = 1", $db);

关于php - 如何为通过 PHP 发送到 android 的 json 数据设置 if 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29878772/

相关文章:

php - 在部署网站之前要做哪些改进?

php - 在函数和多个脚本 (PHP) 中使用单例数据库类 - 最佳使用方法

java - 如何删除整个列表但只留下最后一个索引

android - 如果在 Android 中达到特定日期和时间,我如何在后台检查

android - 启用 Android 支持库调试标志的最佳方式

php - Doctrine DBAL 对存储过程的支持

php - mysql中一次删除多个表

php - PHP 中的 Foreach 循环 - 遍历结果数组,将它们显示在 HTML 中的单个表格行中?

php - mysql在php函数中选择*或所需参数

php - 根据用户登录状态调整购物车中的数组值