[英]how to get the output in array using echo php
嗨,在下面我用echo函数打印组名。但是我想以array的形式返回echo消息。因为我在客户端读取这些echo消息。 例如我的输出如下:
NewGroup-->New is one groupname and Group is the second groupname
例外输出:
{New},{Group}
PHP
case "DispalyGroupDetails" :
$userId = authenticateUser($db, $username, $password);
if ($userId != NULL) {
if (isset($_REQUEST['username'])) {
$username = $_REQUEST['username'];
$sql = "select Id from users where username='$username' limit 1";
if ($result = $db -> query($sql)) {
if ($row = $db -> fetchObject($result)) {
$sql = "SELECT g.id,g.groupname
FROM `users` u, `friends` f, `group` g
WHERE u.Id=f.providerId and
f.providerId=g.providerId
GROUP BY g.id, g.groupname";
$theResult = $db -> query($sql);
if ($theResult) {
while ($theRow = $db -> fetchObject($theResult)) {
echo $theRow -> groupname;
}
$out = SUCCESSFUL;
} else {
$out = FAILED;
}
} else {
$out = FAILED;
}
} else {
$out = FAILED;
}
} else {
$out = FAILED;
}
} else {
$out = FAILED;
}
break;
将组名添加到数组中,然后添加json_encode($array);
。
注意:此代码未经过测试,因为我没有您的数据库。
像这样:
$userId = authenticateUser($db, $username, $password);
$array = array();
if ($userId != NULL) {
if (isset($_REQUEST['username'])) {
$username = $_REQUEST['username'];
$sql = "select Id from users where username='$username' limit 1";
if ($result = $db->query($sql)) {
if ($row = $db->fetchObject($result)) {
$sql = "SELECT g.id,g.groupname FROM `users` u, `friends` f, `group` g WHERE u.Id=f.providerId and f.providerId=g.providerId GROUP BY g.id, g.groupname";
$theResult = $db->query($sql);
if ($theResult) {
$count = 0;
while( $theRow = $db->fetchObject($theResult)) {
$array[$count] = $theRow->groupname;
$count++;
}
$out = SUCCESSFUL;
echo json_encode($array);
} else {
$out = FAILED;
}
} else {
$out = FAILED;
}
} else {
$out = FAILED;
}
} else {
$out = FAILED;
}
}
else {
$out = FAILED;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.