我有一个 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/