如何为通过PHP发送到android的json数据设置if条件
我有一个PHP文件,它从MySQL数据库获取数据,并将其作为json数组发送到android。到目前为止,所有行都被发送到android应用程序。我只想发送如何为通过PHP发送到android的json数据设置if条件,php,android,mysql,json,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 $
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);
SQL查询不会如此自然地设置任何筛选条件—如果该信息位于事件
表的某一列中—您将获得已批准和未批准的事件。尝试类似的方法:SELECT*FROM event WHERE event.approved=1
。我在while循环中添加了这个if($row['approved']==1){
,它可以工作。谢谢你的评论@nhee,它也可以工作