PHP脚本,获取待处理的请求列表

时间:2015-03-16 06:16:37

标签: php android json

我刚刚开始编写PHP脚本来为我的Android应用程序的后端供电。我目前要做的是运行一个PHP脚本,该脚本进入我的数据库send_friendreq和名为pending_req的表,并获取行toUser并将其添加到数组中。我目前唯一的问题是我无法让PHP脚本正常运行。任何帮助,将不胜感激。这是我目前拥有的PHP脚本的代码。非常感谢您的帮助!

if (isset($_POST['Username']) && isset($_POST['FriendReq']))
{
    $username = $_POST['Username'];
    $usernamebeingreq = $_POST['FriendReq'];
    $i=0;

    //$sqlCheck = "SELECT Username FROM Users WHERE Username = '" . $usernamebeingreq . "'";
    //$resultCheck = mysqli_query($con, $sqlCheck);

    //if(!$resultCheck)
    //{
    //echo "Invalid Username";
    //}
    //else
    //{
    $sql="SELECT fromUser FROM pending_req WHERE toUser='&username'";
    $result = mysqli_query($con, $sql);

    $array = array();
    while ($row = pg_fetch_array($result))
    {
         $i++;
    }
    for($x=0;$x<$i;$x++)
    {
        echo $array[$x];
    }
    if(!$result)
    {
        echo 'Failed';

    }
    else
    {
        echo json_encode($array[$x]);
        echo "<br>";
    }

如果您对能够更好/更有效/更安全的产品有任何建议,请告诉我们!

2 个答案:

答案 0 :(得分:1)

如果我没有错,你正试图从你的查询中得到json的结果。试试这段代码。

if (isset($_POST['Username']) && isset($_POST['FriendReq']))
{
$username = $_POST['Username'];
$usernamebeingreq = $_POST['FriendReq'];
$i=0;

//$sqlCheck = "SELECT Username FROM Users WHERE Username = '" . $usernamebeingreq . "'";
//$resultCheck = mysqli_query($con, $sqlCheck);

//if(!$resultCheck)
//{
//echo "Invalid Username";
//}
//else
//{
$sql = 'SELECT fromUser FROM pending_req WHERE toUser='. $username ;
$result = mysqli_query($con, $sql);

if(!$result) {
    echo 'Failed';
}elseif($result){

    $myArray = array();

    while($row = $result->fetch_array(MYSQL_ASSOC)) {
        $myArray[] = $row;
    }

    echo json_encode($myArray);
}

答案 1 :(得分:0)

你查询语法似乎不正确plz修改为:

 $sql="SELECT fromUser FROM pending_req WHERE toUser='$username'";